Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Integer Literals in C++ Initializer List

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 ?

like image 997
thatmarkdude Avatar asked Jul 25 '26 14:07

thatmarkdude


1 Answers

It seems your compiler by default considers the type char similarly to the type signed char.

In these declarations

char myChar {127ULL};     /* OK */

char myChar2 {128ULL};

the initializers have positive values and the positive value 128 can not be represented in an object of the type signed char.

The maximum positive value that can be represented by the type signed char is 127. That is the range of the values is [-128, 127].

So the compiler issues a warning.

For this statement

short int si2 {i}; 

the compiler issues a warning because the initializer is not a compile-time constant.

If you will write

const int i = 0;

then the warning disappears.

like image 54
Vlad from Moscow Avatar answered Jul 28 '26 04:07

Vlad from Moscow



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!