27

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?

sharptooth
  • 167,383
  • 100
  • 513
  • 979

7 Answers7

28

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.

oɔɯǝɹ
  • 7,219
  • 7
  • 58
  • 69
Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
  • 3
    windows.h is compiled without warnings because it is stuffed with `#pragma warning(disable)` inside. – sharptooth Nov 15 '11 at 09:03
  • 4
    I like the anti-silly warning header, although some of them don't look *that* silly... – jbat100 Nov 15 '11 at 09:05
  • @jbat100: What amazes me most is that warnings 4510-4513 look very similar to warnings 4623-4626 which I didn't quite realize before. – sharptooth Nov 15 '11 at 09:43
11

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).

Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
  • 4
    -1 because its addressing only an *example* given by the OP and not the actual question about strategies to deal with all *pointless* warnings – Will Nov 15 '11 at 09:03
  • 2
    @Will In fact the question is specifically about *one* warning (C4127) in *one compiler. Upvoting. – user207421 Nov 15 '11 at 09:31
  • 1
    @EJP "This question is not bound to any specific compiler warning, the following is just an example." ... "Is there any elegant way to get rid of a pointless warning?" – Will Nov 15 '11 at 09:35
  • 2
    @Will I can read, thanks, including the parts you didn't quote. – user207421 Nov 15 '11 at 09:48
6

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.

Community
  • 1
  • 1
Martin Ba
  • 37,187
  • 33
  • 183
  • 337
  • What's *a single line preprocessor construct*? – sharptooth Nov 15 '11 at 12:01
  • 1
    @sharptooth - I was referring to the fact that you only need one line to express your intent of disabling the warning. On VS2005 you have to fiddle with several lines of push/disable/pop – Martin Ba Nov 15 '11 at 12:07
3

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) {
    ...
}
olafure
  • 3,518
  • 2
  • 33
  • 45
0

I use while(true, 1) to suppress this warning.

-1

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
reder
  • 1,098
  • 7
  • 9
  • 1
    I do not see why. The behaviour of `for(;;)` is pure convention. It should be equivalen to `while()` that does not compile. So I hid the ugly `for(;;)`, changed the `while` a bit, to keep close to the original code. – reder Nov 15 '11 at 09:25
  • Well, `for(;;)` is a widely used overengineered construct and what you suggest is a newly introduced overengineered construct. – sharptooth Nov 15 '11 at 09:27
  • 2
    The advice to install Linux and use GCC looks even well comparing to this answer. – Tadeusz Kopec for Ukraine Nov 15 '11 at 10:30
  • Downvote me to death, nobody provided a working example to overcome your problem. for is correct but ugly to some - I just hid it. Disabling warning may make you miss errors (more usually typos I did a bit of stats (on OSS) and about 2/3 of coders use for(;;) whereas the other 1/3 goes for while(true). for has a distinct C-ish flavour, that some may find disgusting (it reminds me of Numerical Recipes...) – reder Nov 15 '11 at 14:29
  • 1
    I really laughed aloud! Thanks @reder! Let's spice those codes! – alcor Mar 20 '14 at 13:25
-2

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.

Peter Wone
  • 17,965
  • 12
  • 82
  • 134
  • In this case the answer was already at the bottom of the list. Further downvotes serve only to demonstrate humourless pettiness. I treated this question as a joke because it is self-evident that the way to avoid warnings is to write code in such a way that they are not triggered. In C# it is possible to suppress a warning with attributes, globally or right down to a specific member. People rarely use this because sweeping hiding warnings is a bad idea. – Peter Wone Apr 04 '13 at 03:20