2

See that example:

//test.cpp
#include <iostream>
void test(unsigned int i, int j) {
  std::cout << i << " " << j << std::endl;
}
int main() {
  test(-1, -1);
  int x = -1;
  test(x,x);
  return 0;
}

with:

$ g++ -Wall -Wextra -Wpedantic test.cpp:
4294967295 -1
4294967295 -1

Why does gcc let that slip? And is there an option to detect such an implicit conversion?

Cheers

Jonas K
  • 168
  • 1
  • 8
  • Possible duplicate of [Can I make GCC warn on passing too-wide types to functions?](http://stackoverflow.com/questions/310108/can-i-make-gcc-warn-on-passing-too-wide-types-to-functions) – Rakete1111 May 06 '17 at 16:06
  • @Rakete1111 `unsigned int` is typically of the same width as `int`. – Walter May 06 '17 at 16:09

2 Answers2

1

Yup, I found it. Misassumed that (-Wall -Wextra -W -Wpedantic -Wconversion) would cover it all. But in

https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html

the missing flag is: -Wsign-conversion

Jonas K
  • 168
  • 1
  • 8
1

This has been answered before. One of the reasons is because C allowed it, and c++ was meant to be backwards compatible. Some compilers will warn, though I tested on gcc 5.2 and it does not have an option to turn that warning on.

See: Why does C++ allows implicit conversion from int to unsigned int?

#

Just found from one of the other answers that you need to add the -Wsign-conversion flag. Seems -Wall should do that but doesn't.

Community
  • 1
  • 1