gcc narrowing conversion in + operator

Viewed 197

I'm trying to compile some fairly simple C++ code with GCC 6, but getting a narrowing conversion warning. This is problematic because we treat warnings as errors.

struct S {
    short int a;
    short int b;
};

short int getFoo();
short int getBar();

std::array<S, 2> arr = {{
    {5, getFoo()},
    {3, getFoo() + getBar()}   // Narrowing conversion here?
}};

You can see this code in action at https://godbolt.org/g/wHNxoc. GCC says getFoo() + getBar() is narrowing from an int down to a short int. What is causing the upcast to an int? Is there any good solution here short of a forced cast to short int?

1 Answers
Related