是`警告C4127`(有条​​件的前pression是不变的)曾经有帮助吗? [英] is `warning C4127` (conditional expression is constant) ever helpful?

查看:272
本文介绍了是`警告C4127`(有条​​件的前pression是不变的)曾经有帮助吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在回答<一个href=\"http://stackoverflow.com/questions/25801230/explanation-needed-for-this-$p$p-processor-directive-c-c/25801260#25801260\">this文章中,我建议使用做{...}而(0)多行宏。

While answering this post, I suggested using do {...} while(0) for multiline macros.

在MSVC,我发现这个code抛出了:

On MSVC, I found this code throws up:

warning C4127: conditional expression is constant

为了使code警告 - 自由,我需要选择这些丑陋的备选方案之一:

To make code warning-free, I need to choose one of these ugly alternatives:

选项1

#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable:4127)
#endif
code_using_macro_that_generates_C4217;
#ifdef _MSC_VER
#pragma warning(pop)
#endif

选项2

我定义为宏:

Option 2
Define my macros as:

#define MULTI_LINE_MACRO do { ... } while(0,0)

#define MULTI_LINE_MACRO do { ... } while((void)0,0)

也称为猫头鹰的一些程序员为(0,0)看起来像一只猫头鹰。

选项3

定义一个新的宏WHILE_0不产生警告,并用它来代替,而(0)

问题

我相信所有的方案都或多或少可怕。为什么MSVC生成此警告看似正确的code和激励我的一些丑恶添加到我的code以保持code警告免费的吗?

Problem
I believe all alternatives are more or less horrible. Why does MSVC generate this warning for seemingly correct code and motivate me to add some ugliness to my code to keep the code warning free?

我相信,在条件语句不断前pressions是完全有效的,有用的,特别是在构造基础上,编译器优化了code的能力。

I believe constant expressions in conditionals are perfectly valid and useful, in particular in constructs based on the compiler's ability to optimize out code.

而且我没有得到一个警告C4127 为code是这样的:

Moreover I don't get a warning C4127 for code like this:

void foo(unsigned bar)
{
    while (bar >= 0)
        ;
} 

我的问题是:是不是警告C4127:有条件的前pression是恒定完全无用,不将其激励丑陋code?该警告过帮助写出更好的code?

My question is: Isn't warning C4127: conditional expression is constant completely useless and doesn't it motivate ugly code? Does this warning ever help writing better code?

推荐答案

我不认为这是以往任何时候都非常有用。相反,有更多的误报只比做..而(0)成语。认为像

I don't think it is ever useful. On the contrary, there are more false positives than only the do .. while(0) idiom. Think of constructs like

if(sizeof(long) == 8) { /* ... */ }
if(SOME_CONSTANT_MACRO) { /* ... */ }

前者不能#如果指令所取代,后者可以,但一些编码风格指南preFER的如果版本语法检查为死者code仍在进行(这是不是死在其他平台上,或与其他编译时配置)和一些发现它更好看。

The former cannot be replaced by #if directives, the latter could, but some coding style guidelines prefer the if version as syntax checking is still done for the dead code (which isn't dead on other platforms or with other compile-time configuration) and some find it nicer to read.

警告(比由标准,其中大部分应视为错误需要其他)通常发射为code是有效的,但很可能会做的比什么意别的东西。 如果(0)或像这样的事情都看傻了,但好像不是语法检查这个否则死code以外的东西是打算不看。它可能困扰着读者,但它是毫不含糊的,我看不出这可能意外发生。

Warnings (other than those required by the standard, most of which should be treated as errors) are usually emitted for code that is valid but is likely to do something else than what's intended. if(0) or things like this look silly, but don't look as if something other than "syntax check this otherwise dead code" was intended. It may puzzle the reader, but it's unambiguous, and I don't see how this could happen accidentally.

这是迄今(我还没有得到MSVC来测试我自己)所举的例子,好像警告是在C语言的意识不断前pressions(也就是说,不是哪个可以是恒定折,但语法是不是一个常量前pression),所以它不会发出了如果(阵列)如果(功能)(如什么的gcc -Wall 并发出警告,因为它很可能打算成为一个函数调用)。

From the examples given thus far (I haven't got MSVC to test on my own), it seems like the warning is for constant expressions in the sense of the C language (that is, not something which can be constant-folded but syntactically isn't a constant expression), so it isn't emitted for if(array), or if(function) (what e.g. gcc -Wall does warn about because it's likely intended to be a function call).

而(0,0)更糟糕的是,在我看来,它会触发与的gcc -Wall 警告对于无副作用的逗号操作符的左侧,警告我能想象是偶尔有用(并且通常是很容易避免的)。该警告与消失,而((无效),0,0)

while(0,0) is worse, in my opinion, it triggers a warning with gcc -Wall for a left-hand side of a comma operator without side-effects, a warning I can imagine to be occasionally useful (and which usually is easy to avoid). This warning disappears with while((void)0,0).

我建议接通警告关闭。

这篇关于是`警告C4127`(有条​​件的前pression是不变的)曾经有帮助吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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