Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between initializing with = and initializing with {}?

Tags:

c++

c++11

You can initialize a variable using = . For example:

int a = 1000;

C++ 11 introduced an extra notation {} . For example:

int a {1000};

According to Programming: Principles and Practices by Bjarne Stroustrup:

C++11 introduced an initialization notation that outlaws narrowing conversions.

I wanted to check out this cool feature. And I typed a piece of code twice:

#include "std_lib_facilities.h"     |     #include "std_lib_facilities.h" 
                                    |
int main()                          |     int main()
                                    |
{                                   |     {
    int x = 254;                    |         int x {254};
    char y = x;                     |         char y {x};
    int z = y;                      |         int z {y};
                                    |
    cout << "x = " << x << '\n'     |         cout << "x = " << x << '\n'
         << "y = " << y << '\n'     |              << "y = " << y << '\n'
         << "z = " << z << '\n';    |              << "z = " << z << '\n';
                                    |
}                                   |     }

The code on the left uses = whereas the code on the right uses {}

But the code on the right hand side loses some information even after using {}. Thus the output is same in both pieces of code:

x = 254

y = ■

z = -2

So, what's the difference between initializing with = and initializing with {} ?

EDIT: My question may or may not be a duplicate. I'm just a beginner and I do not even understand the code of the possibly original question. I'm no judge. Even if it's a duplicate, I cannot understand any answers of that question. I feel that this question should be treated as an original one since I would understand simple language opposed to some high-level words of that question's answer.

like image 819
Superex Avatar asked Mar 30 '16 14:03

Superex


1 Answers

What is the difference between initializing with = and initializing with {}?

In general, the difference is that the former is copy initialization and the latter is direct - list initialization. The linked online reference describes all three forms of initialization in detail. In particular, there is the difference that you already quoted; List initialization may not be used with narrowing conversions.

But the code on the right hand side loses some information even after using {}

The program on the right side is ill-formed.

As mentioned, the standard does not allow narrowing conversions in the context of list initialization. int to char is a narrowing conversion.

Because the program is ill-formed, the standard does not guarantee its behaviour except that a compiler is required to emit a diagnostic message. For example, g++-5.3.0 will say:

warning: narrowing conversion of 'x' from 'int' to 'char' inside { } [-Wnarrowing]

May you please elaborate on the "ill-formed" part?

Standard says that the program shall not do X. Your program does X. Therefore it violates the standard which makes the program ill-formed. Here, X is:

If a narrowing conversion (see below) is required to convert any of the arguments, the program is ill-formed.


The program on the left is not ill-formed. However it, just like the program on the right side, does assign a value to char y that is not representable by the char type (it could be representable in some implementation where char is an unsigned type, but given the output, that appears not to be the case for your implementation). When an unrepresentable value is converted to a signed type, the resulting value is implementation defined.

like image 191
eerorika Avatar answered Sep 29 '22 16:09

eerorika