Christopher Johnson
Christopher Johnson

Reputation: 675

Pure Virtual Destructor with Default Keyword

Is it possible to declare a destructor as pure virtual and use the default keyword? For example, I can't seem to make code like this work:

class MyClass
{
public:
  // Is there a way to combine pure virtual and default?
  virtual ~ MyClass() = 0,default;
};

One can of course later do:

MyClass::~ MyClass() = default;

Also, if the destructor is not pure virtual, the default keyword does work when it follows the declaration.

Upvotes: 3

Views: 533

Answers (2)

senbrow
senbrow

Reputation: 607

No, it is not possible.

By declaring the member function with the = default specifier, you are providing a function definition.

From the working draft of the C++14 standard (N3936):

§ 10.4 Note: A function declaration cannot provide both a pure-specifier and a definition

https://github.com/cplusplus/draft/raw/b7b8ed08ba4c111ad03e13e8524a1b746cb74ec6/papers/N3936.pdf

Upvotes: 2

Lightness Races in Orbit
Lightness Races in Orbit

Reputation: 385144

No.

You will have to write a separate definition and default it there, as you've shown.

The presence of a pure-specifier precludes the presence of a definition at the same location, even when that definition is just a = default.

Upvotes: 7

Related Questions