Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is the initialization below well-formed?

The example in [class.conv.ctor]/2 contains the following initialization:

Z a3 = Z(1);    // OK: direct initialization syntax used

How is this considered a direct-initialization syntax?

like image 769
Wake up Brazil Avatar asked Aug 23 '16 19:08

Wake up Brazil


2 Answers

Z(1) will direct-initialize a prvalue. The prvalue will then be used to initialize an object. By the rules of guaranteed elision, there is no temporary-followed-by-copy. The prvalue initializes the object directly. Therefore, Z a3 = Z(1); is exactly equivalent to Z a3(1);.

In pre-C++17, this would perform direct initialization of a prvalue temporary, followed by a (almost certainly elided) copy of the temporary into the object a3. Whether the copy is elided or not, the initialization of the prvalue is via direct initialization. The initialization of a3 is by copy-initialization, but this is through the copy constructor, which is not explicit.

like image 102
Nicol Bolas Avatar answered Oct 09 '22 16:10

Nicol Bolas


It's talking about Z(1). [dcl.init]/16:

The initialization that occurs in [...] functional notation type conversions (5.2.3) [...] is called direct-initialization.

The prvalue is then used to copy-initialize z, which is just fine, guaranteed elision or not - Z's copy/move constructors aren't explicit anyway, so the initialization is fine even without the guaranteed elision in C++17.

like image 23
T.C. Avatar answered Oct 09 '22 16:10

T.C.