Reputation: 19120
A virtual destructor which does nothing is
virtual ~ClassName() {}
Since C++11 we can alternatively say:
virtual ~ClassName() = default;
Is there any difference between these two?
Upvotes: 10
Views: 622
Reputation: 5557
The main difference is that there are rules for defaulted functions that specify under which circumstances they are deleted (cf. ISO c++14(N4296
) 8.4, 12.1, 12.4, 12.8)
8.4.2.5: Explicitly-defaulted functions and implicitly-declared functions are collectively called defaulted functions, and the implementation shall provide implicit definitions for them (12.1 12.4, 12.8), which might mean defining them as deleted.
e.g.:
12.4.5: A defaulted destructor for a class X is defined as deleted if: (5.1) — X is a union-like class that has a variant member with a non-trivial destructor, (5.2) — any potentially constructed subobject has class type M (or array thereof) and M has a deleted destructor or a destructor that is inaccessible from the defaulted destructor, (5.3) — or, for a virtual destructor, lookup of the non-array deallocation function results in an ambiguity or in a function that is deleted or inaccessible from the defaulted destructor
In case your use falls into one of the deleted categories, using default
will be equivalent to using delete
whereas {}
won't be.
Upvotes: 7