Initialization: brackets and equal sign

Possible duplicate:
Is there a difference in C ++ between copy initialization and assignment initialization?

What is the difference between

T a(b); 

and

 T a = b; 

and

 T a = T(b); 

?

+31
c ++ initialization
Dec 17 2018-10-12
source share
4 answers
 T a( b ); 

- This is direct initialization if it does not parse as a function declaration, in which case it is a function declaration.

 T a = b; 

- This is the initialization of the copy, which means that it works as if the temporary object was built on the right side, and that a then copied either in C ++ 11 and later versions, possibly temporary.

The compiler is free to delete (delete) temporary + copy / move whenever possible, but the copy or move constructor, whatever it is logically used, should be available, not explicit .

For example, in C ++ 03 you cannot copy-initialize std::ostringstream because it does not have a copy constructor. In C ++ 11, you can copy ostringstream initialization if the initializer is temporary, which leads to a logical move construct (which will usually be eliminated, optimized). For example, this is a copy initialization declaration,

 ostringstream s = ostringstream( "blah" ); 

& hellip; does not compile as C ++ 03, because in C ++ 03, initializing a copy calls the constructor of an instance of a class that does not exist. However, it compiles as C ++ 11 because in C ++ 11, initializing the copy calls the move constructor. Although (to preserve the illusion of a stream), std::ostringstream cannot be copied directly, it can be moved.

Another such difference: in C ++ 03, only copy initialization syntax supports italic curly braces initializers, which in C ++ 03 can be used when T is an aggregated type, such as an raw array. In C ++ 11, the curly brackets have been expanded and generalized as uniform initialization syntax, so it can also be used with direct initialization. So, the following direct initialization declaration,

 int v[]{ 3, 1, 4, 1, 5, 9, 2, 6, 5, 4 }; 

& hellip; does not compile as C ++ 03, but compiles as C ++ 11 and later.

Copy Initialization Syntax = is the initial initialization syntax from C.

And in C ++ 11 and later, because of the semantics of movement, it can be used in a much wider range of cases than in C ++ 03, for example, with std::ostringstream .

+23
Dec 17 '10 at 13:13
source share
 T a(b); 

Invokes constructor a , which takes b . (If b is of the same type, then the copy constructor is called).

 T a = b; 

a temporary object of type T is created, created by b . Then the copy constructor is called ( = not the destination in this case and in the following case!).

 T a = T(b); 

Same as above! except that we explicitly created a temporary object.

Please note that the standard allows you to completely exclude temporary copies in the second and third cases. In addition, if b does not have type T , then in the first case T should not have a copy constructor. In the second and third cases, even though the implementation is free to optimize all of this, it still requires an affordable copy constructor. The IIRC standard calls it: copy elision.

+16
Dec 17 '10 at 12:54
source share

These are all constructor calls - the = sign is just syntactic sugar. Exactly which constructors are called to a certain extent before the compiler.

0
Dec 17 '10 at 12:56
source share

The = operator will refer to the default copy constructor if the = operator is overloaded. I believe this would make a shallow copy; assigning the same element values ​​to the first object as the right operator

-3
Dec 17 2018-10-17
source share



All Articles