How to overcome pointless C++ compiler warnings elegantly?

谁都会走 提交于 2019-11-30 10:51:54
Cheers and hth. - Alf

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

Martin Ba

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!