c++c++11language-lawyeruniform-initialization

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


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.


Solution

  • 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