Drew Dormann
Drew Dormann

Reputation: 63820

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?

Upvotes: 5

Views: 1040

Answers (1)

user7860670
user7860670

Reputation: 37587

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

online compiler

Upvotes: 9

Related Questions