gpt4 book ai didi

c++ - 需要将析构函数声明为默认

转载 作者:可可西里 更新时间:2023-11-01 15:22:24 25 4
gpt4 key购买 nike

根据 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++ - 需要将析构函数声明为默认,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33254582/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com