Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does 'A a{};' compile when the default constructor A::A() is deleted? [duplicate]

Here's the code example in question:

struct A {
    A() = delete;
};

int main()
{
//  A a(); // compiles, since it's a function declaration (most vexing parse)
//  A a;   // does not compile, just as expected
    A a{}; // compiles, why? The default constructor is deleted.
}

Try it here with any of the available compilers. I tried with several and didn't find one that gave a compilation error.

like image 565
Lmn Avatar asked Feb 18 '26 19:02

Lmn


1 Answers

This is a current language issue that is very likely to be fixed soon. The proposal that tackles the necessary design change can be found here. From the abstract of the proposal:

C++ currently allows some types with user-declared constructors to be initialized via aggregate initialization, bypassing those constructors. The result is code that is surprising, confusing, and buggy

like image 90
lubgr Avatar answered Feb 20 '26 08:02

lubgr