I am trying to understand the full range of functionality of the {} initializer syntax.
I have compiled the following code with g++:
int i = 0; /* OK */
short int si2 {i}; /* Warning: Narrowing Conversion inside {...} */
char myChar {127ULL}; /* OK */
char myChar2 {128ULL}; /* Warning: Narrowing Conversion inside {...} */
My understanding of the warning for the initialization of si2 is as follows. On my system: - short int is 2 bytes - int is 4 bytes
Because the initializer is twice the size (in bytes) as the LHS, this constitutes narrowing and therefore a compiler warning is given.
However for myChar and myChar2, both initializers have the same datatype: unsigned long long int. I believe that the initialization of myChar2 has occurred because the value of the initializer would be too large for the char datatype.
the same rules do not seem to apply in both of these cases: 1. Failure because initializer datatype too large for initialized variable 2. Failure because value is too large for initialized variable (although RHS datatype is acceptable here)
Is my understanding here correct - does the initializer list behave differently if the argument is an integer literal ?
constexpr int i = 0;
instead? It should make it much simpler to statically determine if the initialization is problematic. – François Andrieux