If the default destructor is needed,but its generation has been
suppressed (e.g.,by defining a move constructor),use=default
.
我无法想象,如果没有明确的默认析构函数在类中具有移动构造函数的代码将不正确.
有人可以告诉我上面的例子吗?
struct S { S() {}; S( S&& ) {}; // move ctor }; int main() { S s; // there is no need to declare dtor explicitly =default }
解决方法
从标准来看,12.4 $4.5析构函数[class.dtor]
4 If a class has no user-declared destructor,a destructor is
implicitly declared as defaulted (8.4). An implicitly-declared
destructor is an inline public member of its class.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.