c++ - Narrowing from literal doesn't cause warning -
int = 0; short b{a}; short c{0};
the compiler gives waring short b{a}
. can understand this, because int
narrowed short
.
however, doesn't give warning short c{0}
, weird me. remember literal integers, type of 0
should @ least int
. narrowing int
short
happening here. why doesn't compiler give warning?
for short c{0};
, narrowing conversion doesn't occur. because 0
constant expression , can stored in short
.
(emphasis mine)
list-initialization limits allowed implicit conversions prohibiting following:
...
conversion integer or unscoped enumeration type integer type cannot represent values of original, except source constant expression value can stored in target type
relevant explanations , examples standard, $8.6.4/7 list-initialization [dcl.init.list]:
(emphasis mine)
a narrowing conversion implicit conversion
...
from integer type or unscoped enumeration type integer type cannot represent values of original type, except source constant expression value after integral promotions fit target type.
[ note: indicated above, such conversions not allowed @ top level in list-initializations. — end note ] [ example:
// ... const int z = 99; // ... char c4{z}; // ok: no narrowing needed unsigned char uc1 = {5}; // ok: no narrowing needed // ... float f2 { 7 }; // ok: 7 can represented float // ...
— end example ]
Comments
Post a Comment