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?
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).
What could be -- realistically implementable -- more elegant that a single line of:
#pragma warning ( suppress : 4127 )
(I can't use this as I'm still on VS2005 where this doesn't work for all warnings.)
Certainly, for your case for(;;)
could be the pragmatic approach, but in general
How to overcome pointless C++ compiler warnings elegantly?
I'd say disable them project wide. (there pointless after all). And, to variate
How to overcome false-positve C++ compiler warnings elegantly?
I'd say, a single preprocessor line seems quite OK already.
My philosophy is that if you make the compiler suppress warnings, put a comment in there and tell why. Even if you think it's silly. Pragma stands out and is visible. It's a fine comment in your code.
When you start skipping comments when suppressing warnings based on your idea on how silly it is, you are heading for potential trouble. Another person working on your code after a year might change it, have trouble and waste precious time hunting it down.
Btw, you are looking for a way of suppressing the warnings without either
- Use pragma
- Use ugly code
- Use project wide suppression
That would be a very hidden suppression method.
If you don't like the look of pragma, use
bool alwaysTrue = true; // to prevent compiler warning C4127
while (alwaysTrue) {
...
}
I use while(true, 1) to suppress this warning.
Do some funny token pasting: if you can swap
while(true)
for
While(true)
then just do the following:
#define while_true for( ;; )
#define While(a) while_##a
There are many cases in which satisfactory code behaves as intended yet generates a slew of warnings. This is why they're warnings rather than errors. The fact that they annoy you is good: warnings are intended to modify your behaviour. You are supposed to mend your ways, not sweep them under the carpet.
The example loop from the question could be written like this:
for (; testExitCondition();)
doSomething();
In more sophisticated cases the loop counter can be used as the state of a state machine (each action is required to update state):
for (int state = stateBegin; state == stateTerminate;)
{
switch (state)
{
case stateBegin:
//elaborate setup
break;
case state_1:
doSomething_1();
break;
case state_2:
doSomething_2();
break;
case state_n:
doSomething_n();
break;
}
My favourite way to avoid pointless C++ warnings is to use C# :)
Admittedly this produces a slew of pointless C# warnings but nothing's perfect.
Gosh, look at the negative votes. Proof that C++ damages your sense of humour.
来源:https://stackoverflow.com/questions/8133615/how-to-overcome-pointless-c-compiler-warnings-elegantly