为什么 const int 适合 char brace init?

Why is const int fine for char brace init?

我认为大括号初始化不允许缩小。但为什么 int const 允许 char 大括号初始化?

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

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

看到了on Godbolt.

const int value2 = 12;

value2 是编译时常量。编译器可以轻松(并且必须)证明该值为 12,恰好在 char.

可表示的值范围内
int value1 = 12;

value1 不是编译时常量。变量的值可能会在运行时更改。

标准规则的确切措辞(引用最新草案,强调已加):

[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.