Wake up Brazil Wake up Brazil -4 years ago 134
C++ Question

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?

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download