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


1049. Copy elision through reference parameters of inline functions

Section: 11.9.6  [class.copy.elision]     Status: open     Submitter: Jason Merrill     Date: 2010-03-10

Consider the following example:

    int c;

    struct A {
       A() { ++c; }
       A(const A&) { ++c; }
    };

    struct B {
       A a;
       B(const A& a): a(a) { }
    };

    int main() {
       (B(A()));
       return c - 1;
    }

Here we would like to be able to avoid the copy and just construct the A() directly into the A subobject of B. But we can't, because it isn't allowed by 11.4.5.3 [class.copy.ctor] bullet 34.3:

The part about not being bound to a reference was added for an unrelated reason by issue 185. If that resolution were recast to require that the temporary object is not accessed after the copy, rather than banning the reference binding, this optimization could be applied.

The similar example using pass by value is also not one of the allowed cases, which could be considered part of issue 6.

CWG 2023-05-12

This is a plausible optimization opportunity whose detailed specification requires a paper.