C++ Logo

std-discussion

Advanced search

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

From: Marc Mutz <marc.mutz_at_[hidden]>
Date: Wed, 04 Dec 2019 12:27:51 +0100
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:30:16