简体   繁体   中英

Warning about tautological compares inhibited by const?

The following piece of code generates warnings on both GCC and Clang:

int main() {
  unsigned n = 0;
  return ( n < 0 ) ? 1 : 0;
}

The warnings are:

$ g++-4.7 -std=c++11 -O3 -Wall -Wextra t.cc -o t
t.cc: In function ‘int main()’:
t.cc:3:16: warning: comparison of unsigned expression < 0 is always false [-Wtype-limits]
$ clang++-3.2 -std=c++11 -O3 -Wall -Wextra t.cc -o t
t.cc:3:14: warning: comparison of unsigned expression < 0 is always false [-Wtautological-compare]
  return ( n < 0 ) ? 1 : 0;
           ~ ^ ~
1 warning generated.
$ 

So far, so good. Now I change the variable to be const :

int main() {
  const unsigned n = 0;
  return ( n < 0 ) ? 1 : 0;
}

And both compilers are suddenly happy to compile the code without warnings:

$ g++-4.7 -std=c++11 -O3 -Wall -Wextra t.cc -o t
$ clang++-3.2 -std=c++11 -O3 -Wall -Wextra t.cc -o t
$ 

QUESTION: Why does this happen? Is there a reason , why const variables suppress the warning? If both GCC and Clang agree, I hesitate to just throw a bug-report at them as it seems more likely that I need to learn something :)

EDIT: The constant folding of the compiler might have to do with it, but it is not sufficient to explain the behaviour . In the first example (without the const ), the compiler does know the value and that it never changes. I checked the assembler output and the compiler does do constant folding but it still generates the warning, probably before it replaces the variable by the known constant when it sees the expression ( n < 0 ) and knowing that n is an unsigned type. That said: Why does this behaviour changes when I add const ? I think that if the first example produces a warning, it should also be possible to generate a warning for the second.

It's an explicit constant -- your intent . Your desired warning is effectively transferred from:

warning: comparison of unsigned expression < 0 is always false [-Wtautological-compare]
return ( n < 0 ) ? 1 : 0;
         ~ ^ ~

to -Wunreachable-code when const :

warning: will never be executed [-Wunreachable-code]
return ( n < 0 ) ? 1 : 0;
                   ^

Note: -Wtautological-compare warnings may still be emitted by the compiler where values are not known.

When the variable is not const , it might change, the compiler doesn't know it. That's why there is a warning. If it is const , it will never change. That way, the compiler can replace any occurence of n with the actual value of n , in this case:

return ( 0 < 0 ) ? 1 : 0;
-> return 0;

Hence, no warning.

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