I have a code like this :
#include <iostream>
using std::cout;
using std::endl;
int main() {
uint16_t a = 0;
uint16_t b = 0;
if ( a - b < 3u )
{
cout << "cacahuète" << endl;
}
return 0;
}
When I compile it using g++ with -Wall, I get :
temp.cpp: In function ‘int main()’:
temp.cpp:9:13: warning: comparison of integer expressions of different signedness: ‘int’ and ‘unsigned int’ [-Wsign-compare]
9 | if ( a - b < 3u )
| ~~~~~~^~~~
That warning doesn't show up if I write if ( a - b < static_cast<uint16_t>(3u) ) instead.
- So what's going on, here ? Where does the int come from?
- Can this actually result in an incorrect behavior?
- Is there a less verbose way to silence it? (or a less verbose way to write a uint16_t literal?)