This question is not bound to any specific compiler warning, the following is just an example.
Currently when I want a loop that checks an exit condition inside:
while( true ) { doSomething(); if( condition() ) { break; } doSomethingElse(); }
I can't just write that in Visual C++ - it will emit a C4127 conditional expression is constant
warning. The compiler will wave it in my face although it's quite obvious that while(true)
can't have been written accidentially.
Suppose I want code that compiles without warnings. There're workarounds at my service.
Workaround one is to use for(;;)
but it feels stupid - why would I want that weird thing instead of concise elegant idiomatic while(true)
? Workaround two is to use #pragma warning( suppress)
before while( true )
line but it adds a huge banner that is twice as big as the while
-statement itself. Workaround three is to disable C4127 for the entire project (I've seen it done in a real project) but then all possible useful instances of C4127 are also disabled.
Is there any elegant way to get rid of a pointless warning?
To answer your question about disabling specific warnings in GCC, you can enable specific warnings in GCC with -Wxxxx and disable them with -Wno-xxxx. From the GCC Warning Options: You can request many specific warnings with options beginning -W , for example -Wimplicit to request warnings on implicit declarations.
They are not errors from the viewpoint of a programming language, but they may be software bugs. However, many compilers can be customized so that their warnings don't stop the compilation process. Warnings must not be ignored. You'd better fix every possible error before starting software testing.
You can use the -Werror compiler flag to turn all or some warnings into errors. Show activity on this post. You can use -fdiagnostics-show-option to see the -W option that applies to a particular warning.
I'd write for(;;)
because that's idiomatic.
Newer versions of Visual C++ are not as idiot as earlier versions were wrt. warnings. Visual C++ 10.0 compiles code that uses <windows.h>
, at warning level 4, no warnings.
But if you want to turn off Visual C++ sillywarnings, take a look at my old anti-sillywarnings header, which was created with input from the [comp.lang.c++] community.
I'd go with for(;;)
even without that warning. It's not stupid: it's a loop with no condition, which is exactly what you want to express.
That seems more logical to me than using a while loop and testing that true is still true each time round the loop (of course the compiler will optimize away this test, so it won't actually affect performance).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With