c – 需要将析构函数声明为默认值

前端之家收集整理的这篇文章主要介绍了c – 需要将析构函数声明为默认值前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
根据 these准则:

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.

猜你在找的C&C++相关文章