c4127

Why MSVC generates warning C4127 when constant is used in “while” - C

这一生的挚爱 提交于 2019-12-10 12:45:49
问题 For code, while(1) { /* ..... */ } MSVC generates the following warning. warning C4127: conditional expression is constant MSDN page for the warning suggests to use for(;;) instead of while(1) . I am wondering what advantage for(;;) is giving and why it warns for the constant usage in while ? What flag should I use on GCC to get the same warning? 回答1: Constant conditionals are often enough simply bugs. Consider this: unsigned k; ... while (k>=0) { ... } The condition k>=0 would make sense if

is `warning C4127` (conditional expression is constant) ever helpful?

梦想的初衷 提交于 2019-11-29 13:26:37
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)

is `warning C4127` (conditional expression is constant) ever helpful?

烈酒焚心 提交于 2019-11-28 07:00:49
问题 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