Title
Copy elision for direct-initialization with a conversion function
Status
drafting
Section
9.4 [dcl.init]
Submitter
Richard Smith

Created on 2016-09-30.00:00:00 last changed 91 months ago

Messages

Date: 2023-06-13.20:24:54

Consider an example like:

  struct Cat {};
  struct Dog { operator Cat(); };

  Dog d;
  Cat c(d);

This goes to 9.4 [dcl.init] bullet 17.6.2:

Otherwise, 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 (12.2.2.4 [over.match.ctor]), and the best one is chosen through overload resolution (12.2 [over.match]). 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.

Overload resolution selects the move constructor of Cat. Initializing the Cat&& parameter of the constructor results in a temporary, per 9.4.4 [dcl.init.ref] bullet 5.2.1.2. This precludes the possitiblity of copy elision for this case.

This seems to be an oversight in the wording change for guaranteed copy elision. We should presumably be simultaneously considering both constructors and conversion functions in this case, as we would for copy-initialization, but we'll need to make sure that doesn't introduce any novel problems or ambiguities.

See also issue 2311.

This issue is addressed by paper P2828.

History
Date User Action Args
2016-09-30 00:00:00admincreate