Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is const int fine for char brace init?

I thought brace initialization doesn't allow narrowing. But why is int const allowed for char brace initialization?

int value1 = 12;
char c1{value1};  // error! no narrowing

const int value2 = 12;
char c2{value2};   // why is this fine?

See it on Godbolt.

like image 442
BK C. Avatar asked Sep 09 '19 13:09

BK C.


1 Answers

const int value2 = 12;

value2 is a compile-time constant. The compiler can easily (and has to) prove that the value is 12 which happens to be within the range of values representable by char.

int value1 = 12;

value1 is not a compile-time constant. The value of the variable could change at runtime.

The exact wording of the standard rule (quoting latest draft, emphasis added):

[dcl.init.list]/7

A narrowing conversion is an implicit conversion

  • from an integer type or unscoped enumeration type to an integer type that cannot represent all the values of the original type, except where the source is a constant expression whose value after integral promotions will fit into the target type.
like image 68
eerorika Avatar answered Oct 29 '22 04:10

eerorika