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

            Bug ID: 114123
           Summary: list-initialization with a single element
           Product: gcc
           Version: 13.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: yx_liu at hotmail dot com
  Target Milestone: ---

https://godbolt.org/z/fPd4q7zMd

The issue happens with gcc 13.2 and trunk. The following is the code:

```
#include <vector>
#include <stdio.h>
using namespace std;

struct A {
    int x;
    A(int x_) : x(x_) {printf("%p : A(int %d)\n", this, x);}
    A(const A& a) { x = a.x; printf("%p : A(const A& %p)\n", this, &a);}
    A(const vector<A>& a) { printf ("%p : vector<A>& %p\n", this, &a);}
};

int main() {
    vector<A> a{1,2};
    vector<A> b{a};

    printf("%ld\n", b.size());
}
```

Based on my understanding of https://cplusplus.github.io/CWG/issues/1467.html,

If T is a class type 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).

b should be direct-initialized by a, i.e. equivalent to 

vector<A> b(a);

I would expect the copy ctor of vector<A> to be called, and the elements of a
will be copied to b, and b.size() will be 2.

However, A(const vector<A>& a) is called to contruct b and b.size() is 1.

Currently clang trunk has the expected behavior and b.size() is 1.

https://godbolt.org/z/dh7d5x81T

A few days ago when clang tried to implement CWG2137 (
https://github.com/llvm/llvm-project/pull/77768). It showed the same behavior
as gcc. However that PR was reverted and clang went back to the expected
behavior.

Reply via email to