While answering this post, I suggested using do {...} while(0)
for multiline macros.
On MSVC, I found this code throws up:
warning C4127: conditional expression is constant
To make code warning-free, I need to choose one of these ugly alternatives:
Option 1
#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable:4127)
#endif
code_using_macro_that_generates_C4217;
#ifdef _MSC_VER
#pragma warning(pop)
#endif
Option 2
Define my macros as:
#define MULTI_LINE_MACRO do { ... } while(0,0)
or
#define MULTI_LINE_MACRO do { ... } while((void)0,0)
Also called an "owl" by some programmers as (0,0)
looks like an owl.
Option 3
Define a new macro WHILE_0 which does not generate a warning and use it instead of while(0)
Problem
I believe all alternatives are more or less horrible. Why does MSVC generate this warning for seemingly correct code and motivate me to add some ugliness to my code to keep the code warning free?
I believe constant expressions in conditionals are perfectly valid and useful, in particular in constructs based on the compiler's ability to optimize out code.
Moreover I don't get a warning C4127
for code like this:
void foo(unsigned bar)
{
while (bar >= 0)
;
}
My question is: Isn't warning C4127: conditional expression is constant
completely useless and doesn't it motivate ugly code? Does this warning ever help writing better code?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…