This is an unofficial snapshot of the ISO/IEC JTC1 SC22 WG21 Core Issues List revision 116a. See http://www.open-std.org/jtc1/sc22/wg21/ for the official list.

2024-12-19


2311. Missed case for guaranteed copy elision

Section: 12.2.2.8  [over.match.list]     Status: open     Submitter: Richard Smith     Date: 2016-08-09

Consider:

  struct X {
    X();
  };
  X make();
  X x{make()}; 

We reach 9.4.5 [dcl.init.list] bullet 3.7:

Otherwise, if T is a class type, constructors are considered. The applicable constructors are enumerated and the best one is chosen through overload resolution (12.2 [over.match], 12.2.2.8 [over.match.list]).

This means we perform a redundant copy. If T were an aggregate, 9.4.5 [dcl.init.list] bullet 3.2 would avoid the redundant copy:

If T is an aggregate class and the initializer list has a single element of type cv U, where U is T or a class derived from T, the object is initialized from that element (by copy-initialization for copy-list-initialization, or by direct-initialization for direct-list-initialization).

See also issues 2137 and 2327.