3
votes

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.

3
Right, thanks. The standard indeed says > a function declaration cannot provide both a pure-specifier and a definitionChristopher Johnson
@T.C. Answers go down there, bub. ↓ ↓ ↓ ↓ ↓ ↓Lightness Races in Orbit
default a virtual destuctor ... What for ? It's virtual so defaulting it won't make it trivial and nothing changes for your class from defining it as { }Nikos Athanasiou

3 Answers

7
votes

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.

2
votes

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

-4
votes

This question is sort of a non-starter, since you can only have one destructor anyway. Why would you need to add a default specifier?