Home > conditional expression > error c4127 conditional expression is constant

Error C4127 Conditional Expression Is Constant

Contents

here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn c4127 template more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags

Conditional Expression Is Constant While True

Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you,

Conditional Expression Is Constant Do While 0

helping each other. Join them; it only takes a minute: Sign up C4127: Conditional Expression is Constant up vote 9 down vote favorite The following code generates warning C4127 (conditional expression is constant) in Visual Studio 2010 (where

C4127 Sizeof

alias_wchar_t is an alias for wchar_t): if (sizeof(alias_wchar_t) == sizeof(wchar_t)) // warning occurs here { // do stuff } else { // do other stuff } What's the most elegant way to resolve this, short of suppressing the warning? The best solution I've come up with is to stuff the conditional into a static bool, and use that as the condition. There's a goodly amount of code above and below the if-else, so I wrap the pragma warning disable 4127 whole thing in braces to limit the scope of the variable as much as possible: // { static bool isSameSize = (sizeof(alias_wchar_t) == sizeof(wchar_t)); if (isSameSize) { // do stuff } else { // do other stuff } } // This feels pretty gross though. This seems like it should be resolvable at compile-time rather than runtime, but the preprocessor doesn't know about sizeof. Is there a cleaner, more elegant way to resolve this? c++ visual-studio-2010 c4127 share|improve this question edited Mar 11 '15 at 11:44 Adriano Repetti 38.5k966116 asked Aug 29 '14 at 18:17 Christopher Berman 493417 1 note: C++17 is considering something like if constexpr which would solve this problem –M.M Mar 22 at 5:12 add a comment| 5 Answers 5 active oldest votes up vote 5 down vote accepted It looks like you know what is going on, and you are fine with this. Compiler pragmas are meant for cases like that: __pragma(warning(push)) __pragma(warning(disable:4127)) if (sizeof(alias_wchar_t) == sizeof(wchar_t)) { __pragma(warning(pop)) } Essentially, you are telling the compiler (and even more importantly, to human readers of your code) that you have reviewed the warning, and that you know what you are doing. share|improve this answer answered Aug 29 '14 at 18:24 dasblinkenlight 455k39492842 3 Personally I think Christopher Berman's original solution is better. Pragmas are compiler specific and excessively verbo

here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more while (0,0) about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users c4512 Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping c4127 toner each other. Join them; it only takes a minute: Sign up is `warning C4127` (conditional expression is constant) ever helpful? up vote 13 down vote favorite While answering this post, I suggested using do {...} while(0) for multiline http://stackoverflow.com/questions/25573996/c4127-conditional-expression-is-constant 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. http://stackoverflow.com/questions/28985515/is-warning-c4127-conditional-expression-is-constant-ever-helpful 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? c++ c visual-c++ c4127 share|improve this question edited Mar 18 '15 at 11:45 psmears 12.3k22837 asked Mar 11 '15 at 11:31 Mohit Jain 24.5k83973 7 @Bathsheba: but for(;;) has not the same meaning as do{...}while(0) !!!! –Basile Starynkevitch Mar 11 '15 at 11:34 6 Remove the pragma and #include "no_sillywarnings_please.h" –Keine Lust Mar 11 '15 at 11:38 2 @GiulioFranco: It doesn't solve what do { ... } while(0) solves. if(foo) for(;;) { ...; break; }; else bar(); is a syntax error. –mafso Mar 11 '15 at 11:46 2 @BoykoPerfanov #define ABC(X) {something;} will fail compilation if called as: if(co

Programming Boards C++ Programming "C4127: conditional expression is constant" Huh? Getting started with C or C++ | C Tutorial | C++ Tutorial | C and C++ FAQ | Get a compiler | Fixes for common problems Thread: http://cboard.cprogramming.com/cplusplus-programming/100325-c4127-conditional-expression-constant-huh.html "C4127: conditional expression is constant" Huh? Thread Tools Show Printable Version Email this Page… Subscribe to this Thread… Display Linear Mode Switch to Hybrid Mode Switch to Threaded Mode 03-13-2008 #1 cpjust View Profile View Forum Posts and http://forums.devshed.com/programming-42/vs2010-warning-c4127-conditional-expression-constant-810845.html the hat of sweating Join Date Aug 2007 Location Toronto, ON Posts 3,545 "C4127: conditional expression is constant" Huh? Is my compiler crazy? (OK, it's VC++ 6.0, so that might be a trick question). Code such as the following conditional expression gives me C4127: conditional expression is constant Code: ASSERT(m_reader.m_szSourceCurrent + nNumEndSkip >= m_szSourceCurrent); ASSERT(this == &pNode->GetParent()); ASSERT(it != m_children.end()); How can those conditions possibly be constant?? 03-13-2008 #2 vart View Profile View Forum Posts "I Win!" by U. Lose Join Date Oct 2006 Location Rishon LeZion, Israel Posts 6,758 It depends on the code but if for example in the line above you have initialized m_reader.m_szSourceCurrent = m_szSourc; and nNumEndSkip is unsigned int then first condition will be conditional expression is always true To be or not to be == true 03-13-2008 #3 cpjust View Profile View Forum Posts and the hat of sweating Join Date Aug 2007 Location Toronto, ON Posts 3,545 I don't really see how, since nNumEndSkip can be any number, but I wonder if it has something to do with this being in an inline function? Code: inline void Bookmark::GetSubString(String& strString, int nNumEndSkip) { ASSERT(m_reader.m_szSourceCurrent + nNumEndSkip >= m_szSourceCurrent); ... } 03-13-2008 #4 anon View Profile View Forum Posts The larch Join Date May 2006 Posts 3,573 Doesn't this warning tell you that an expression will always evaluate to true or false (that is, there is no real point in evaluating it at all)? C4127 I might be wrong. Thank you, anon. You sure know how to recognize different types of trees from quite a long way away. Quoted more than 1000 times (I hope). 03-13-2008 #5 vart View Profile View Forum Posts "I Win!" by U. Lose Join Date Oct 2006 Location Rishon LeZion, Israel Posts 6,758 but I wonder if it has something to do with this being in an inline function? I suppose so - compiler looks at the code there the parameters of the function are initialized - and probably makes some optimization - resulting in the conditional expression to be constant To be or not to be == true 03-13-2008 #6 cpjust Vie

Search Username Password Remember Me? Register Lost Password? facebook google twitter rss Free Web Developer Tools Advanced Search  Forum Programming Languages C Programming [C++ VS2010] warning C4127: conditional expression is constant Thread: [C++ VS2010] warning C4127: conditional expression is constant Share This Thread  Tweet This + 1 this Post To Linkedin Subscribe to this Thread  Subscribe to This Thread April 29th, 2011,01:06 AM #1 No Profile Picture EmperorXYZ View Profile View Forum Posts  Registered User Devshed Newbie (0 - 499 posts)  Join Date Apr 2011 Posts 12 Rep Power 0 [C++ VS2010] warning C4127: conditional expression is constant I've been getting that warning and in that specific case, the expression should be a constant. I do not want to use #pragma warning (disable : 4127) because there might be other places where I would genuinely make a mistake. I was wondering if there is a way around it. For example, I found a way to not get warnings about unreferenced variables with UNREFERENCED_PARAMETER(P). Is there something similar with this warning? Faq Reply With Quote April 29th, 2011,02:15 AM #2 clifford View Profile View Forum Posts Visit Homepage  Contributing User                Join Date Aug 2003 Location UK Posts 5,119 Rep Power 1805 The point is that if the condition is a constant, the result is either always true or always false, so the conditional code is either always executed or never executed. So the compiler is telling you that the condition, and if false, the associated code block are entirely redundant You could solve this "problem" in one of two ways (at least): Code: volatile bool run_code = ; if( run_code ) { // do stuff } Code: #if // do stuff #endif The other solutions are either to include the code unconditionally, or remove it altogether; depending what the condition will always be. Faq Reply With Quote April 29th, 2011,04:28 AM #3 No Profile Picture EmperorXYZ View Profile View Forum Posts  Registered User Devshed Newbie (0 - 499 posts)  Join Date Apr 2011 Posts 12 Rep Power 0 Okay thanks. Volatile solved it. Actually, I realize I should have asked the question this way. Say I have const bool FULL_SCREEN = false; You can see why

 

Related content

assignment in conditional expression error

Assignment In Conditional Expression Error table id toc tbody tr td div id toctitle Contents div ul li a href Assignment In Conditional Expression Javascript a li li a href Can t Assign To Conditional Expression a li li a href Syntax Error In Conditional Expression Unexpected Token a li ul td tr tbody table p resources Windows Server resources Programs MSDN subscriptions Overview Benefits Administrators Students Microsoft Imagine Microsoft Student Partners ISV Startups TechRewards Events Community relatedl Magazine Forums Blogs Channel Documentation APIs and reference jslint expected conditional expression instead saw assignment Dev centers Retired content Samples We re