Re: [boost] A PP trick to define a synchronized block java-like macro

________________________________________ De: boost-bounces@lists.boost.org [boost-bounces@lists.boost.org] En nombre de Sebastian Redl [sebastian.redl@getdesigned.at] Enviado el: martes, 20 de enero de 2009 22:39 Para: boost@lists.boost.org Asunto: Re: [boost] A PP trick to define a synchronized block java-like macro
vicente.botet wrote:
for (VARS DECLARATION, bool __continue=true; __continue; __continue=false)
I'm afraid this is not valid syntax. The first clause of the for-statement is a for-init-statement, which can be either an expression with a semicolon, or a simple-declaration. A simple-declaration lets you declare any number of variables, but they must all have the same declspec, i.e. the same fundamental type (with variations like "pointer to", "array of" or "function returning").
The following variation would do: for (VARS DECLARATION, *continue_hlp_,**continue_=&continue_hlp_; continue_; continue_=0) Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

JOAQUIN M. LOPEZ MUÑOZ wrote:
________________________________________ De: boost-bounces@lists.boost.org [boost-bounces@lists.boost.org] En nombre de Sebastian Redl [sebastian.redl@getdesigned.at] Enviado el: martes, 20 de enero de 2009 22:39 Para: boost@lists.boost.org Asunto: Re: [boost] A PP trick to define a synchronized block java-like macro
vicente.botet wrote:
for (VARS DECLARATION, bool __continue=true; __continue; __continue=false)
I'm afraid this is not valid syntax. The first clause of the for-statement is a for-init-statement, which can be either an expression with a semicolon, or a simple-declaration. A simple-declaration lets you declare any number of variables, but they must all have the same declspec, i.e. the same fundamental type (with variations like "pointer to", "array of" or "function returning").
The following variation would do:
for (VARS DECLARATION, *continue_hlp_,**continue_=&continue_hlp_; continue_; continue_=0)
This is brilliant! Weird workaround at its best. Sebastian

Sorry, I have no compiled this at all. Thanks Sebasian for pointing me this C++ feature that I ignored completly. And Joaquin, bravo for the trick. The macros defined on the article use a wrapper of the variable that has two methods done_post_step and post_step, but the Joaquin solution seems to me much better. It is a pleasure to exchange on the Boost ML. Thanks to boths, Vicente ----- Original Message ----- From: "Sebastian Redl" <sebastian.redl@getdesigned.at> To: <boost@lists.boost.org> Sent: Tuesday, January 20, 2009 10:53 PM Subject: Re: [boost] A PP trick to define a synchronizedblock java-like macro JOAQUIN M. LOPEZ MUÑOZ wrote:
________________________________________ De: boost-bounces@lists.boost.org [boost-bounces@lists.boost.org] En nombre de Sebastian Redl [sebastian.redl@getdesigned.at] Enviado el: martes, 20 de enero de 2009 22:39 Para: boost@lists.boost.org Asunto: Re: [boost] A PP trick to define a synchronized block java-like macro
vicente.botet wrote:
for (VARS DECLARATION, bool __continue=true; __continue; __continue=false)
I'm afraid this is not valid syntax. The first clause of the for-statement is a for-init-statement, which can be either an expression with a semicolon, or a simple-declaration. A simple-declaration lets you declare any number of variables, but they must all have the same declspec, i.e. the same fundamental type (with variations like "pointer to", "array of" or "function returning").
The following variation would do:
for (VARS DECLARATION, *continue_hlp_,**continue_=&continue_hlp_; continue_; continue_=0)
This is brilliant! Weird workaround at its best.

JOAQUIN M. LOPEZ MUÑOZ wrote:
________________________________________ De: boost-bounces@lists.boost.org [boost-bounces@lists.boost.org] En nombre de Sebastian Redl [sebastian.redl@getdesigned.at] Enviado el: martes, 20 de enero de 2009 22:39 Para: boost@lists.boost.org Asunto: Re: [boost] A PP trick to define a synchronized block java-like macro
vicente.botet wrote:
for (VARS DECLARATION, bool __continue=true; __continue; __continue=false)
I'm afraid this is not valid syntax. The first clause of the for-statement is a for-init-statement, which can be either an expression with a semicolon, or a simple-declaration. A simple-declaration lets you declare any number of variables, but they must all have the same declspec, i.e. the same fundamental type (with variations like "pointer to", "array of" or "function returning").
The following variation would do:
for (VARS DECLARATION, *continue_hlp_,**continue_=&continue_hlp_; continue_; continue_=0)
Or you could do: if (bool stop_ = false) {} else for (VARS DECLARATION; !stop_; stop_ = true) which I'd consider more readable (Thanks to BOOST_FOREACH for this trick). John Bytheway

Quoting John Bytheway <jbytheway+boost@gmail.com>:
if (bool stop_ = false) {} else for (VARS DECLARATION; !stop_; stop_ = true)
which I'd consider more readable (Thanks to BOOST_FOREACH for this trick).
John Bytheway
The other trick in this part of the book is to use nested fors, which can give you slightly more flexibility. FOREACH does this too, IIRC.

----- Original Message ----- From: "Peter Bartlett" <pete@pcbartlett.com> To: <boost@lists.boost.org> Sent: Wednesday, January 21, 2009 9:46 AM Subject: Re: [boost] A PP trick to define a synchronized blockjava-like macro
Quoting John Bytheway <jbytheway+boost@gmail.com>:
if (bool stop_ = false) {} else for (VARS DECLARATION; !stop_; stop_ = true)
which I'd consider more readable (Thanks to BOOST_FOREACH for this trick).
John Bytheway
The other trick in this part of the book is to use nested fors, which can give you slightly more flexibility. FOREACH does this too, IIRC.
Hi, Thanks John for the if else suggestion. I like it. Could some of you point where FOREACH use these tricks? Thanks, Vicente

Quoting "vicente.botet" <vicente.botet@wanadoo.fr>:
----- Original Message ----- From: "Peter Bartlett" <pete@pcbartlett.com> To: <boost@lists.boost.org> Sent: Wednesday, January 21, 2009 9:46 AM Subject: Re: [boost] A PP trick to define a synchronized blockjava-like macro
Quoting John Bytheway <jbytheway+boost@gmail.com>:
if (bool stop_ = false) {} else for (VARS DECLARATION; !stop_; stop_ = true)
which I'd consider more readable (Thanks to BOOST_FOREACH for this trick).
John Bytheway
The other trick in this part of the book is to use nested fors, which can give you slightly more flexibility. FOREACH does this too, IIRC.
Hi,
Thanks John for the if else suggestion. I like it. Could some of you point where FOREACH use these tricks?
The main BOOST_FOREACH macro definition (line 1059 in foreach.hpp in version 1.37) uses four nested ifs. It uses a nested for too - the last for is the "real" for loop!
participants (5)
-
JOAQUIN M. LOPEZ MUÑOZ
-
John Bytheway
-
Peter Bartlett
-
Sebastian Redl
-
vicente.botet