简体   繁体   中英

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

There's a piece of code at the top of LightZ.cpp that goes like this:

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

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. So this would appear to be a problem with VS2005. For what it's worth, VS2008 does exactly the same thing.

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:

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

I tested it on my VS2005 and the warning does not appear, even at warning level 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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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