简体   繁体   English

在VS2005下出现奇怪的“警告C4127:条件表达式恒定”

[英]odd “warning C4127: conditional expression is constant” under VS2005

I'm trying to compile LightZPng with warnings on level 4. I get a lot of C4127 on lines that are clearly not worthy of this warning. 我正在尝试使用级别4的警告编译LightZPng 。我在显然不值得该警告的行上得到了很多C4127。 An example: 一个例子:

#define MAX_BITS 15
int values_per_bitlen[ MAX_BITS + 1 ];
for ( int i = 0; i <= MAX_BITS; ++i )    // C4127 is here
    values_per_bitlen[ i ] = 0;

How can this code be changed to avoid the warning other than #pragma? 除了#pragma之外,如何更改此代码以避免警告?

There's a piece of code at the top of LightZ.cpp that goes like this: 在LightZ.cpp的顶部有一段代码是这样的:

#define for if (false) {} else for

That means your actual statement is: 这意味着您的实际陈述是:

#define for if (false) {} else for ( int i = 0; i <= MAX_BITS; ++i )

which is why you're getting the constant expression error (it's the false , not the i <= MAX_BITS as I thought). 这就是为什么您遇到常量表达式错误的原因(它是false ,而不是我想的i <= MAX_BITS )。

Simply comment out or delete that line from the file (I can't actually figure out why they would do that). 只需注释掉该行或从文件中删除该行(我实际上无法弄清楚他们为什么会这样做)。

Yes, that its odd. 是的,这很奇怪。 It's truly not a constant expression since i changes in the loop. 因为i在循环中更改,所以它实际上不是常数表达式。 So this would appear to be a problem with VS2005. 因此,VS2005似乎是一个问题。 For what it's worth, VS2008 does exactly the same thing. 就其价值而言,VS2008所做的事情完全相同。

Strangely enough, a project with just this in it does not complain so it may well be some weird edge-case problem with Microsoft's warning generation code: 奇怪的是,一个仅包含此内容的项目不会抱怨,因此它很可能是Microsoft警告生成代码的一些奇怪的极端情况:

#define MAX_BITS 15
int values_per_bitlen[ MAX_BITS + 1 ];
int main(int argc, char* argv[]) {
    for ( int i = 0; i <= MAX_BITS; ++i )
        values_per_bitlen[ i ] = 0;
    return 0;
}

However, you haven't actually asked a question. 但是,您实际上没有问过一个问题。 What is it that you want to know, or want us to do? 您想知道或希望我们做什么?

Update: 更新:

See "Windows programmer"'s answer for the actual cause - there's a "#define for if (false) {} else for" at the top of LightZ.cpp which is causing the problem. 实际原因请参见“ Windows程序员”的答案-导致问题的LightZ.cpp顶部有一个"#define for if (false) {} else for"

I tested it on my VS2005 and the warning does not appear, even at warning level 4. . 我在VS2005上对其进行了测试,即使在警告级别4上也不会出现警告。

A simple procedure for you to follow : 您可以按照以下简单步骤操作:

-Create a new Console App and place only the above code and see if the warning shows up again. -创建一个新的控制台应用程序,并仅放置上面的代码,然后查看警告是否再次出现。

-If not, check for differences in the project settings. -如果没有,请检查项目设置中的差异。

-If yes, I would assume that your optimization setting may be causing it. -如果是,我认为您的优化设置可能是造成这种情况的原因。

According to Charles Nicholson , Visual Studio 2005 gives this error with the " do...while(0) " trick: 根据Charles Nicholson的说法 ,Visual Studio 2005通过“ do...while(0) ”技巧给出了此错误:

#define MULTI_LINE_MACRO \
    do { \
        doSomething(); \
        doSomethingElse(); \
    } while(0)

If you absolutely must, you can use the __pragma directive to selectively disable that warning around a particular code fragment. 如果绝对必要,则可以使用__pragma指令来有选择地禁用特定代码片段附近的警告。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM