Convert gcc restriction to + operator

I am trying to compile fairly simple C ++ code with GCC 6, but I get a narrower conversion warning. This is problematic because we view 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?
}};

This code can be seen at https://godbolt.org/g/wHNxoc . GCC says getFoo () + getBar () narrows from int down to short int. What causes a raise to int? Is there any good solution here besides force casting to short int?

+4
source share
1 answer

This is due to integrated advertising :

(, char) prvalues ​​ (, int). , , , int as , lvalue-to-rvalue, . .

, getFoo() + getBar() int, .

, static_cast:

static_cast<short>(getFoo() + getBar())
+8

Source: https://habr.com/ru/post/1680442/


All Articles