如何克服毫无意义的 C++优雅地编译器警告?

发布于 2024-12-15 17:47:16 字数 706 浏览 0 评论 0原文

这个问题不受任何特定编译器警告的约束,以下只是一个示例。

目前,当我想要一个检查内部退出条件的循环时:

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

我不能只用 Visual C++ 编写它 - 它会发出 C4127 条件表达式为常量 警告。编译器会在我面前挥舞它,尽管很明显 while(true) 不可能是意外编写的。

假设我想要编译时不发出警告的代码。我可以为您提供解决方法

解决方法是使用 for(;;) 但感觉很愚蠢 - 为什么我想要那个奇怪的东西而不是简洁优雅的惯用 while(true)代码>? 解决方法二是在 while( true ) 行之前使用 #pragma warning(suppress) ,但它会添加一个巨大的横幅,其大小是while 语句本身。 解决方法三是为整个项目禁用 C4127(我在实际项目中见过它),但随后 C4127 的所有可能有用的实例也被禁用。

有没有什么优雅的方法来摆脱毫无意义的警告?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

乖乖 2024-12-22 17:47:17

做一些有趣的标记粘贴:如果你可以交换

while(true)

While(true)

那么只需执行以下操作:

#define while_true for( ;; )
#define While(a) while_##a

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
归途 2024-12-22 17:47:17

在很多情况下,令人满意的代码按预期运行,但会生成大量警告。这就是为什么它们是警告而不是错误。他们惹恼你这一事实是好事:警告的目的是改变你的行为。你应该改正自己的行为,而不是把它们掩盖起来。

问题中的示例循环可以这样写:

for (; testExitCondition();) 
  doSomething();

在更复杂的情况下,循环计数器可以用作状态机的状态(每个操作都需要更新状态):

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;
}

我最喜欢的方式避免无意义的 C++ 警告的方法是使用 C# :)

诚然,这会产生大量无意义的 C# 警告,但没有什么是完美的。

天哪,看看反对票。证明 C++ 会损害你的幽默感。

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.

最佳男配角 2024-12-22 17:47:16

我会写 for(;;) 因为这是惯用的。

Visual C++ 的新版本并不像早期版本那样白痴。警告。 Visual C++ 10.0 编译使用 的代码,警告级别为 4,无警告。

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

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.

夕嗳→ 2024-12-22 17:47:16

即使没有这个警告,我也会选择 for(;;) 。这并不愚蠢:这是一个没有条件的循环,这正是您想要表达的。

对我来说,这似乎比使用 while 循环并在每次循环时测试 true 仍然为 true 更符合逻辑(当然编译器会优化此测试,因此它实际上不会影响性能)。

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

青瓷清茶倾城歌 2024-12-22 17:47:16

什么可能 - 实际可实现 - 优雅的单行:(

 #pragma warning ( suppress : 4127 )

我不能使用它,因为我仍在VS2005上,其中这个并不适用于所有人警告。)

当然,对于您的情况 for(;;) 可能是务实的方法,但总的来说

如何优雅地克服毫无意义的C++编译器警告?

我会说禁用它们 项目范围。 (毕竟没有意义)。并且,要变化

如何优雅地克服误报 C++ 编译器警告?

我想说,单个预处理器行似乎已经很不错了。

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.

时光病人 2024-12-22 17:47:16

我的理念是,如果您让编译器抑制警告,请在其中添加注释并说明原因。即使你认为这很愚蠢。
Pragma 脱颖而出并且可见。这是您代码中的一个很好的注释。

当你开始跳过评论并根据你的想法压制警告时,你就会遇到潜在的麻烦。一年后处理您的代码的另一个人可能会更改它,遇到麻烦并浪费宝贵的时间来寻找它。

顺便说一句,您正在寻找一种抑制警告的方法,而无需

  • 使用编译指示
  • 使用丑陋的代码
  • 使用项目范围的抑制

这将是一种非常隐藏的抑制方法。

如果您不喜欢 pragma 的外观,请使用

bool alwaysTrue = true; // to prevent compiler warning C4127
while (alwaysTrue) {
    ...
}

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) {
    ...
}
王权女流氓 2024-12-22 17:47:16

我使用 while(true, 1) 来抑制此警告。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文