简体   繁体   中英

Difference in initializing char-array and char with integer literals

On my system ( 4.13.11-1-ARCH, gcc 7.2.0 ) char is signed . When initializing an array of char with an integer literal like this:

const char mydata[] = {
    0x80
};

I get the following error:

error: narrowing conversion of ‘128’ from ‘int’ to ‘char’ inside { } [-Wnarrowing]

However, when I instead do const char data = 0x80 the compiler is not worried of any narrowing, although it happens of course. The output is 7F , the highest positive signed char value.

Question

Why is the compiler not equally worried about truncation in both cases?

This is actually one of the reasons why {} initialization should be prefered: It does not allow narrowing conversions. In contrast to this, the old way of initalization (as in const char data = 0x80 ) does allow narrowing conversions.

An initializer of the form const char c = 0x80 is a much older construct than initializer lists, which have been introduced later. So it was possible to define stricter rules for initializer lists, while these rules where not applied to "older" initializers (probably because of not to break "older" code more than necessary).

Hence, initializer lists as defined in this online c++ standard draft forbid such narrowing:

8.5.1 Aggregates

(2) When an aggregate is initialized by an initializer list, as specified in [dcl.init.list], the elements of the initializer list are taken as initializers for the members of the aggregate, in increasing subscript or member order. Each member is copy-initialized from the corresponding initializer-clause. If the initializer-clause is an expression and a narrowing conversion ([dcl.init.list]) is required to convert the expression, the program is ill-formed. ...

BTW: If you use a brace initializer like const char data { 0x80 } , you will get an error, too. So the stricter rules are due to brace-initializers / initializer lists, and not due to whether you initialize an array or a scalar value.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM