https://gcc.gnu.org/bugzilla/show_bug.cgi?id=106599

            Bug ID: 106599
           Summary: Wrong copy elision in delegating to copy-constructor
           Product: gcc
           Version: 12.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: fchelnokov at gmail dot com
  Target Milestone: ---

The following program should be valid:
```
struct A {       
    int v = 0;
    constexpr A() = default;
    constexpr A(const A&) : v(1) {}
    constexpr A(int) : A(A()) {}
};

static_assert( A(2).v == 1 );
```
and is accepted by Clang but not GCC. Online demo:
https://gcc.godbolt.org/z/zKoqq3rKW

Clang is probably correct here, because
http://eel.is/c++draft/class.base.init#7 says:

The expression-list or braced-init-list in a mem-initializer is used to
initialize the designated subobject (or, in the case of a delegating
constructor, the complete class object) according to the initialization rules
of [dcl.init] for direct-initialization.

So we should use the rules for direct-initialization and
http://eel.is/c++draft/dcl.init#general-16.6.2 says:

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

So we need to consider the constructors, and select A(const A&) : v(1)

Reply via email to