0

I have such code

struct Foo {
    Foo() {}

    Foo(std::initializer_list<Foo>) {
        std::cout << "initializer list" << std::endl;
    }

    Foo(const Foo&) {
        std::cout << "copy ctor" << std::endl;
    }
};

int main() {
    Foo a;
    Foo b(a);
    Foo c{a};
}

Compiling it with g++ -std=c++20 -o main main.cpp and running gives me such output:

copy ctor
copy ctor
initializer list

But running binary compiled with clang++ -std=c++20 -o main main.cpp outputs the following:

copy ctor
copy ctor

So why are those two outputs are different?

wowonline
  • 1,061
  • 1
  • 10
  • 17
  • While the initializer list will gladly overtake other (potentially) matching parameterized constructors, I'm not sure why you expect this code to *not* call the copy constructor. – sweenish May 03 '23 at 18:51
  • 1
    clang compiler has determined that initilizer-list was not required in this situtation (you only passed a single value), so copy was sufficient, change to `Foo c{a,a};` and you'll see that initializer-list will now get called on both compilers – user20716902 May 03 '23 at 18:55
  • 1
    This is [CWG2137](https://wg21.link/CWG2137) (clang does not seem to have implemented this yet, gcc is right) – Artyer May 03 '23 at 19:14

0 Answers0