Title
Copy elision through reference parameters of inline functions
Status
open
Section
11.9.6 [class.copy.elision]
Submitter
Jason Merrill

Created on 2010-03-10.00:00:00 last changed 19 months ago

Messages

Date: 2023-05-12.20:59:33

CWG 2023-05-12

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

Date: 2022-02-18.07:47:23

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:

  • when a temporary class object that has not been bound to a reference (6.7.7 [class.temporary]) would be copied/moved to a class object with the same cv-unqualified type, the copy/move operation can be omitted by constructing the temporary object directly into the target of the omitted copy/move

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.

History
Date User Action Args
2023-05-12 20:59:33adminsetmessages: + msg7277
2010-03-10 00:00:00admincreate