How to make gcc warn about narrowing function parameters

2

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?

c++
c
gcc
compiler-warnings
asked on Stack Overflow Jan 5, 2018 by Drew Dormann • edited Jan 5, 2018 by Drew Dormann

1 Answer

6

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

online compiler

answered on Stack Overflow Jan 5, 2018 by user7860670 • edited Jan 5, 2018 by user7860670

User contributions licensed under CC BY-SA 3.0