Search code examples
c++gcccompiler-errorsunsigned-integer

Is there a way to make GCC alert me when I provide a signed integer to function that accepts only unsigned one?


This code gives me no warnings when compiled with g++:

unsigned int myFunc(unsigned int integer) {
  return integer;
}
int main() {
  int x = -1;
  std::cout << myFunc(x) << std::endl;
}

It compiles fine but the result is wrong: 4294967295. Does GCC have any compiler -W* options for that?


Solution

  • Yes, -Wsign-conversion

    Beware that -Wconversion doesn't enable this warning for C++ code, although it does for C code.