简体   繁体   中英

Does deleted destructor change aggregate initialization in C++?

The code as follows

struct B {
    ~B() = delete;
};

B * b = new B{};

fails to compile in the latest MSVC with the error:

error C2512: 'B': no appropriate default constructor available
note: Invalid aggregate initialization

At the same time both GCC and Clang do not see anything wrong in the code, demo: https://gcc.godbolt.org/z/va9vcsEed

Is it right to assume just a bug in MSVC?

Overall, does the presence or deletion of the destructor change any rule of the aggregate initialization?

Neither definition of the notion of aggregate in C++ Standards refers to the destructor.

For example the definition of an aggregate in C++ 20 (9.4.2 Aggregates) sounds the following way

1 An aggregate is an array or a class (Clause 11) with

(1.1) — no user-declared or inherited constructors (11.4.5),

(1.2) — no private or protected direct non-static data members (11.9),

(1.3) — no virtual functions (11.7.3), and

(1.4) — no virtual, private, or protected base classes (11.7.2).

If to execute this statement in MS VS 2019

std::cout << std::is_aggregate_v<B> << '\n';

then the output will be 1 .

On the other hand, the default constructor is defined as deleted (the C++ 20 Standard, 11.4.5.2 Default constructors) if

(2.8) — any potentially constructed subobject has a type with a destructor that is deleted or inaccessible from the defaulted default constructor.

But in the provided example there is no such sub-object.

So it seems it is a compiler bug of MS VS 2019.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM