Can g ++ warn when passing a negative literal as an unsigned parameter?

Consider:

unsigned foo(unsigned u) { return u; } int main() { foo(-1); return 0; } 

Here the function foo is called with u equal to 4294967295 (or with a similarly large value.) If the programmer did not pay attention, this can be quite unexpected.

For example, you might be implementing pow to raise your Polynomial class. Since only positive credentials are possible, you decide to sign

 Polynomial pow(const Polynomial& p, unsigned exp); 

Then the careless programmer calls pow(p, -1) to get the opposite, and instead of warning or error, it seems to work, but it probably uses an extremely large amount of memory and time to get a completely wrong answer.

g ++ 5.3.0 and gcc 5.3.0, compile this without complaint with -Wall -Wextra .

They will warn about this with the -Wsign-conversion option, but it warns of every conversion from int to unsigned and is very annoying (it warns every time you index a vector with int , vec[i] , for example.)

Can gcc warn only about sending a negative literal or other negative compile-time constant as an unsigned parameter?

+5
source share

All Articles