1
votes

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?

2
You can only provide definitions for functions that you declared yourself... there's no such thing as giving a non-inline body for an implicitly-declared function - M.M
@MattMcNabb Yeah, I've understood it from answer. But what's the point to mark such functions as default. They've declared by a compiler already. - user2953119
@DmitryFucintv: There is no need (assuming you are not doing anything which causes the function to be deleted). Some people do it because they want to be very explicit about what functions are available in their classes. A lot of people don't approve of implicitly declared functions, and would prefer if every function was required to be explicit. - Benjamin Lindley
That's quite a different question to what you just posted. (a) marking it as default means the function exists when it might not have (e.g. move-constructor for class with user-declared destructor); (b) self-documentation - M.M
@DmitryFucintv: you're misunderstanding... it can be deleted or defaulted or user-defined. The text you quote just uses "defined as defaulted* as a short-hand for the implicitly defined copy constructor behaviour. - Tony Delroy

2 Answers

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

A::A(){ }; //here you can define default constructor

int main()
{
 A a;
}

you have defined explicit destructor not constructor , add constructor declarartion and define it as outside clas

5
votes

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