C++ Logo

std-discussion

Advanced search

Re: Initialising a non-aggregate from {} works - why?

From: language.lawyer_at <language.lawyer_at_[hidden]>
Date: Wed, 4 Dec 2019 14:38:48 +0300
https://stackoverflow.com/questions/26964221/does-copy-list-initialization-invoke-copy-ctor-conceptually

On 04/12/2019 14:27, Marc Mutz via Std-Discussion wrote:
> Hi,
>
> According to my reading of the C++14 standard, this should not work:
>
> struct A {
> A() = default;
> A(const A&) = delete;
> A(A&&) = delete;
> virtual f() {} // make it non-aggregate
> };
> A a1 = {}; // works
> A a2 = A{}; // doesn't
>
> A f1() { return {}; } // works
> A f2() { return A{}; } // doesn't
>
> I understand why the "// doesn't" lines don't work. But why does {} work?
>
> The return statement is supposed to be copy-initialisation, which, up to and incl. C++14 needed to check the viability of the copy/move ctor. It can't be aggregate initialisation, either, because I make A explicitly a non-aggregate.
>
> All of MSVC, GCC, and Clang accept this code: https://godbolt.org/z/S3HgaJ
>
> What's going on?
>
> Thanks,
> Marc

Received on 2019-12-04 05:41:14