需要将析构函数声明为默认
Need for declare destructor as default
根据 these 准则:
If the default destructor is needed, but its generation has been
suppressed (e.g., by defining a move constructor), use =default
.
我无法想象在 class 中没有显式默认析构函数的代码何时会格式错误 class 具有移动构造函数。
谁能给我举个例子来证实上面的引述?
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.
根据 these 准则:
If the default destructor is needed, but its generation has been suppressed (e.g., by defining a move constructor), use
=default
.
我无法想象在 class 中没有显式默认析构函数的代码何时会格式错误 class 具有移动构造函数。
谁能给我举个例子来证实上面的引述?
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.