Search code examples
c++constructordefault-constructor

Why does a class with a user-declared destructor have an implicitly defaulted constructor?


Code:

struct A
{ 
    ~A(){ };
};

A::A(){ }; //error: definition of implicitly declared default constructor

int main()
{
    A a;
}

DEMO

Why does the code produces the error? I expected that the program compiles fine. The Standard says N3797::12.8/7 [class.copy]:

If the class definition does not explicitly declare a copy constructor, one is declared implicitly. If the class definition declares a move constructor or move assignment operator, the implicitly declared copy constructor is defined as deleted; otherwise, it is defined as defaulted (8.4). The latter case is deprecated if the class has a user-declared copy assignment operator or a user-declared destructor.

It's a bug or my misunderstanding?


Solution

  • You may not define an implicitly declared constructor by the compiler.

    From the C++ Standard (12 Special member functions)

    1. ... Programs shall not define implicitly-declared special member functions