Avoiding warning on the do{...}while(0) macro implementation trick

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? Could this approach be used to stop disabling warning 4127 when boost is compiled? Do other compilers have similar warnings and workarounds? Should a boost macro that expands to a warning-free always-false (or always-true) compile-time constant be considered? This would also allow statements such as BOOST_ASSERT(BOOST_FALSE), while(BOOST_TRUE), or do{...}while(BOOST_FALSE) to compile without warning... Regards, Ivan -- http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form

"Ivan Vecerina" wrote:
do{ }while(__LINE__==-1)
Do you see a problem with this approach?
No.
Could this approach be used to stop disabling warning 4127 when boost is compiled?
Yes, probably.
Do other compilers have similar warnings and workarounds?
BCB often complains. It may be applicable here too.
Should a boost macro that expands to a warning-free always-false (or always-true) compile-time constant be considered? This would also allow statements such as BOOST_ASSERT(BOOST_FALSE), while(BOOST_TRUE), or do{...}while(BOOST_FALSE) to compile without warning...
I often use my macro CANNOT_HAPPEN and leave it in production code to get info on impossible-to-happen errors. If one could globally modify semantics of BOOST_ASSERT(BOOST_FALSE) to call similar macro or be nop or whatever it would be handy. /Pavel
participants (2)
-
Ivan Vecerina
-
Pavel Vozenilek