RE: [boost] Avoiding warning on the do{...}while(0) macroimplementation trick

Ivan Vecerina wrote:
I've noticed discussions about this a couple of years ago. For assert-style macros that include multiple statements, a common idiom is to enclose the statements in a do{...}while(0) dummy loop. This makes the macro behave like a single statement.
A problem with some compilers (notably VC7.x) is that this triggers a warning (C4127: conditional expression is constant).
The workaround adopted by boost is to automatically disable this warning -- which however can be legitimate and useful in user code.
I think I have found a better 'always-false' condition which does not trigger a warning, and works at least in VC7.1: do{ }while(__LINE__==-1)
Do you see a problem with this approach? <snip>
It works in VC++ 7.1 if you enable "edit and continue", because this results in non-standard expansion of the __LINE__ macro to an expression that isn't a compile-time constant. In general it doesn't work.

Ben Hutchings <ben.hutchings <at> businesswebsoftware.com> writes:
I think I have found a better 'always-false' condition which does not trigger a warning, and works at least in VC7.1: do{ }while(__LINE__==-1) Do you see a problem with this approach? <snip> It works in VC++ 7.1 if you enable "edit and continue", because this results in non-standard expansion of the __LINE__ macro to an expression that isn't a compile-time constant. In general it doesn't work.
Why not something like: class aa { public: operator bool() const { return false; } }; do {} while(aa()); Optimizing this shouldn't be a problem for most compilers...

"Goran Mitrovic" wrote:
Why not something like:
class aa { public: operator bool() const { return false; } };
do {} while(aa());
Optimizing this shouldn't be a problem for most compilers...
BCB won't optimize it away. Version for dumb compilers should be provided too. /Pavel
participants (3)
-
Ben Hutchings
-
Goran Mitrovic
-
Pavel Vozenilek