Stupid C++ Tricks: do/while(0) and C4127

March 1, 2009
Tags: , , , ,

A nice little quickie:  I briefly discuss in my assert ramblings why it’s important to wrap all of your multi-line macros in do/while(0) blocks.  An unfortunate side-effect of this is that the construction

// NOISY CODE
#define MULTI_LINE_MACRO \
do { \
std::printf(“Hello “); \
std::printf(“world!\n”); \
} while (0)

will trigger C4127: “Conditional expression is constant” in Visual Studio 2003/2005, and [...]

16