Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the motivation behind having copy and direct initialization behave differently?

Somewhat related to Why is copy constructor called instead of conversion constructor?

There are two syntaxes for initialization, direct- and copy-initialization:

A a(b); A a = b; 

I want to know the motivation for them having different defined behavior. For copy initialization, an extra copy is involved, and I can't think of any purpose for that copy. Since it's a copy from a temp, it can and probably will be optimized out, so the user can't rely on it happening - ergo the extra copy itself isn't reason enough for the different behavior. So... why?

like image 366
Luchian Grigore Avatar asked Jun 27 '12 09:06

Luchian Grigore


People also ask

What is meant by copy initialization?

Copy initialization is performed in the following situations: 1) when a named variable (automatic, static, or thread-local) of a non-reference type T is declared with the initializer consisting of an equals sign followed by an expression.

What is assignment and copy initialization?

Direct Initialization or Assignment Operator (Syntax) This assigns the value of one object to another object both of which are already exists. Copy initialization is used when a new object is created with some existing object.

What is direct initialization?

Direct-initialization is more permissive than copy-initialization: copy-initialization only considers non-explicit constructors and non-explicit user-defined conversion functions, while direct-initialization considers all constructors and all user-defined conversion functions.


2 Answers

Only a speculation, but I am afraid it will be hard to be more certain without Bjarne Stroustrup confirming how it really was:

It was designed this way because it was assumed such behaviour will be expected by the programmer, that he will expect the copy to be done when = sign is used, and not done with the direct initializer syntax.

I think the possible copy elision was only added in later versions of the standard, but I am not sure - this is something somebody may be able to tell certainly by checking the standard history.

like image 157
Suma Avatar answered Oct 01 '22 00:10

Suma


Since it's a copy from a temp, it can and probably will be optimized out

The keyword here is probably. The standard allows, but does not require, a compiler to optimize the copy away. If some compilers allowed this code (optimized), but others rejected it (non-optimized), this would be very inconsistent.

So the standard prescribes a consistent way of handling this - everyone must check that the copy constructor is accessible, whether they then use it or not.

The idea is that all compilers should either accept the code or reject it. Otherwise it will be non-portable.


Another example, consider

A a; B b;  A a1 = a; A a2 = b; 

It would be equally inconsistent to allow a2 but forbid a1 when As copy constructor is private.


We can also see from the Standard text that the two methods of initializing a class object were intended to be different (8.5/16):

If the initialization is direct-initialization, or if it is copy-initialization where the cv-unqualified version of the source type is the same class as, or a derived class of, the class of the destination, constructors are considered. The applicable constructors are enumerated (13.3.1.3), and the best one is chosen through overload resolution (13.3). The constructor so selected is called to initialize the object, with the initializer expression or expression-list as its argument(s). If no constructor applies, or the overload resolution is ambiguous, the initialization is ill-formed.

Otherwise (i.e., for the remaining copy-initialization cases), user-defined conversion sequences that can convert from the source type to the destination type or (when a conversion function is used) to a derived class thereof are enumerated as described in 13.3.1.4, and the best one is chosen through overload resolution (13.3). If the conversion cannot be done or is ambiguous, the initialization is ill-formed. The function selected is called with the initializer expression as its argument; if the function is a constructor, the call initializes a temporary of the cv-unqualified version of the destination type. The temporary is a prvalue. The result of the call (which is the temporary for the constructor case) is then used to direct-initialize, according to the rules above, the object that is the destination of the copy-initialization. In certain cases, an implementation is permitted to eliminate the copying inherent in this direct-initialization by constructing the intermediate result directly into the object being initialized; see 12.2, 12.8.

A difference is that the direct-initialization uses the constructors of the constructed class directly. With copy-initialization, other conversion functions are considered and these may produce a temporary that has to be copied.

like image 27
Bo Persson Avatar answered Oct 01 '22 00:10

Bo Persson