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

You can initialize a variable using =. For instance:

int a = 1000; 

In C ++ 11, the additional notation {} is introduced. For instance:

 int a {1000}; 

According to the Program: Principles and Practices of Bjarna Straustrup:

C ++ 11 introduced initialization notation, which prohibits narrowing conversions.

I wanted to check out this great feature. And I typed the code snippet 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 =, while the code on the right uses {}

But the code on the right side loses some information even after using {}. Thus, the output is the same for both parts of the 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 don’t even understand the code of a possible original question. I am not a judge. Even if this is a duplicate, I cannot understand the answers to this question. I believe that this question should be considered as original, since I would understand a simple language, the opposite of some high-level words of the answer to this question.

+5
source share
1 answer

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

In general, the difference is that the first copy initialization , and the last direct - list initialization . The linked online link details all three forms of initialization. In particular, there is a difference that you have already quoted; List initialization cannot be used with narrowing conversions.

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

The program on the right side is poorly formed.

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

Since the program is poorly formed, the standard does not guarantee its behavior, except that the compiler must issue a diagnostic message. For example, g ++ - 5.3.0 will say:

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

Can you talk about the "poorly formed" part?

The standard says that the program should not do X. Your program does X. Therefore, it violates the standard, which makes the program poorly formed. Here X:

If converting any of the arguments requires narrowing the transform (see below), the program is poorly formed.


The program on the left is not badly formed. However, it, like the program on the right side, assigns a value of char y , which is not represented by type char (it can be represented in some implementation, where char is an unsigned type, but given the output, which seems to be not suitable for your implementation ) When an unrepresentable value is converted to a signed type, the resulting value is determined by the implementation.

+7
source

Source: https://habr.com/ru/post/1246080/


All Articles