Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make gcc warn about narrowing function parameters

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?

like image 318
Drew Dormann Avatar asked Mar 07 '23 05:03

Drew Dormann


1 Answers

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

online compiler

like image 170
user7860670 Avatar answered Mar 15 '23 03:03

user7860670