5

The program below involves a function parameter that is implicitly narrowed. Information is potentially lost.

void func(short) {}

int main()
{
    int i = 0x7fffffff;
    func(i);
}

If I compile this program (either as C or C++) with gcc using -Wall -Wextra I receive no warnings!

Surely, this behavior would often be considered undesirable.

Is there some gcc command-line parameter that would trigger a diagnostic message when these narrowing conversions occur?

Drew Dormann
  • 59,987
  • 13
  • 123
  • 180
  • 1
    the option: `-Wconversion` will allow the compiler to output warning messages about such problems. – user3629249 Jan 05 '18 at 19:45
  • I was just concerned that the compiler might interpret `0xffffffff` as -1, and somehow realize that the value would fit into a short. It was a long shot. – Mark Ransom Jan 05 '18 at 19:46
  • this question is about `gcc`, not about `C++` (which is a different language) so please remove the `c++` tag – user3629249 Jan 05 '18 at 19:47
  • MSVC objects to something else: *error C2055: expected formal parameter list, not a type list*. That would be OK for the function **prototype** `void func(short);` but not for its implementation, since the code cannot use the argument without an identifier. It was not a warning, but an error. – Weather Vane Jan 05 '18 at 20:06

1 Answers1

9

Use -Wconversion for gcc/clang. /W4 can be used for VC++.

online compiler

user7860670
  • 35,849
  • 4
  • 58
  • 84