Take the following code
#include <iostream>
template<typename T>
T f(T x, unsigned y) {
if (y < 0) return x;
return static_cast<T>(0);
}
using namespace std;
int main() {
int a = f(2, 3);
std::cout << a << std::endl;
return 0;
}
where function f
clearly always returns 0. Compiling it with g++-7.2.0 -Wall -Wextra
gives no hint about pointless comparison. However, clang warns us nicely:
a.cpp:7:11: warning: comparison of unsigned expression < 0 is always false [-Wtautological-compare]
if (y < 0) return x;
~ ^ ~
1 warning generated.
Why is this so (I presume templates are the root of the problem) and can gcc
be forced to output the warning in this case?