Does deleted destructor change aggregate initialization in C++?
Asked Answered
H

1

6

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?

Husserl answered 11/12, 2021 at 10:17 Comment(3)
Neither definition of the aggregate in C++ Standards refers to the destructor. So it is a bug of the compiler.Garver
Bug verified with the VS2022 beta - still doesn't compile - Fedor, will you file a bug report?Overlying
Thanks, yes, I have reported the bug: developercommunity.visualstudio.com/t/…Husserl
G
7

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.

Garver answered 11/12, 2021 at 10:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.