C++ Logo

std-proposals

Advanced search

Re P0889

From: Maciej Cencora <m.cencora_at_[hidden]>
Date: Thu, 12 Mar 2020 23:07:29 +0100
Hi,

unless I am missing something, the problem with optimal object construction
with user-defined constructor is still unsolved in your proposal.
I.e.
when we have an aggregate:
struct Foo
{
    Bar1 a;
    Bar2 b;
};

Following expression is optimal w.r.t. construction (no copy/moves for
Bar1/Bar2) thanks to aggregate initialization:
Foo f = { Bar1{...}, Bar2{...} }; // #1


But if user-defined constructor is provided, then there is no way to
achieve optimal construction with same initialization expression as in #1.

struct Foo
{
   Foo(Bar1 a, Bar2 b) #2
    : a(std::move(a)), b(std::move(b))
   {}

   Foo(const Bar&1 a, const Bar2& b) #3
    : a(a), b(b)
   {}

   Bar1 a;
   Bar2 b;
};
You get a temporary construction, and either move(#2) or copy(#3) +
temporary destruction.

Ultimate copy ellision, should relax rules of copy elision so that any
copy/moves of constructor arguments into object members/bases is allowed as
well.
So that the most simple constructor definition is always optimal:

struct Foo
{
   Foo(Bar1 a, Bar2 b)
    : a(a), b(b)
   {}

   Bar1 a;
   Bar2 b;
};

Regards,
Maciej

Received on 2020-03-12 17:10:29