virtual destructor=default 和空体有什么区别吗?

Is there any difference between virtual destructor=default and one with empty body?

什么都不做的虚拟析构函数是

virtual ~ClassName() {}

自 C++11 起 we can alternatively say:

virtual ~ClassName() = default;

这两个有什么区别吗?

主要区别在于默认函数有规则指定在何种情况下删除它们(参见 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.

例如:

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

如果您的使用属于已删除的类别之一,使用 default 将等同于使用 delete{} 则不是。