如何克服无意义的C ++编译器警告优雅? [英] How to overcome pointless C++ compiler warnings elegantly?

查看:231
本文介绍了如何克服无意义的C ++编译器警告优雅?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题不限于任何特定的编译器警告,以下只是一个例子。

This question is not bound to any specific compiler warning, the following is just an example.

目前当我想要一个循环检查退出条件: / p>

Currently when I want a loop that checks an exit condition inside:

 while( true ) {
    doSomething();
    if( condition() ) {
       break;
    }
    doSomethingElse();
}



我不能只写在Visual C ++中 - 它会发出一个C4127 条件表达式是常量警告。编译器会在我的脸上波动,虽然很明显 while(true)不能被意外写入。

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.

解决方法是使用 ;;)但它感觉很愚蠢 - 为什么我想要那个奇怪的东西,而不是简洁的优雅惯用 while(true)解决方法是在之前使用 #pragma warning(suppress) while(true)但它增加了一个巨大的横幅,它的大小是的两倍,而 - 语句本身。 解决方法是为整个项目禁用C4127(我看到它在一个真正的项目中完成),但随后C4127的所有可能有用的实例也被禁用。

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?

推荐答案

新版本的Visual C ++不像以前的版本那样笨拙,因为它是惯用的。警告。 Visual C ++ 10.0编译使用< windows.h> 的警告级别4,无警告的代码。

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.

但是如果你想关闭Visual C ++愚蠢的警告,请查看我的旧反愚蠢警告标题,它是使用[comp.lang.c ++]社区的输入创建的。

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.

这篇关于如何克服无意义的C ++编译器警告优雅?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆