Why gcc warns about narrowing conversion only for uniform initialization?

后端 未结 2 1688
深忆病人
深忆病人 2020-12-06 20:51

I am trying to convert long type variable to int type variable with uniform initialization and without it. But I get compiler warning only with uni

2条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-06 21:20

    Because the standard says, narrowing conversions limit is specified only for list initialization (since C++11).

    list-initialization limits the allowed implicit conversions by prohibiting the following:

    • conversion from a floating-point type to an integer type
    • conversion from a long double to double or to float and conversion from double to float, except where the source is a constant expression and overflow does not occur
    • conversion from an integer type to a floating-point type, except where the source is a constant expression whose value can be stored exactly in the target type
    • conversion from integer or unscoped enumeration type to integer type that cannot represent all values of the original, except where source is a constant expression whose value can be stored exactly in the target type

    For the other initialization methods (using parentheses or equal sign), narrowing conversions limit rule is not applied (added); because that might break much legacy code.

提交回复
热议问题