隐式使用析构函数

阿维·基西夫(Avi Kivity)

我有一个带有删除的析构函数的类(实际上,它需要外部帮助才能被销毁):

struct indestructible {
    indestructible(indestructible&&);
    ~indestructible() = delete;
};

当我尝试使用其move构造函数时,编译器抱怨:

struct user {
   indestructible ind;
   user(indestructible&& ind) : ind(std::move(ind)) {}
};

indestructible.cc:11:3: error: attempt to use a deleted function
user(indestructible&& ind) : ind(std::move(ind)) {}
^
indestructible.cc:6:3: note: '~indestructible' has been explicitly marked  deleted here
    ~indestructible() = delete;

这是怎么回事?没有其他成员可以抛出该异常,构造函数主体也没有抛出该异常,那么为什么移动构造函数调用析构函数会导致任何原因?

TC

[class.base.init] / 12

在非委托构造函数中,可能会调用每个直接或虚拟基类以及每个类类型的非静态数据成员的析构函数(12.4)。[注意:此规定确保在引发异常的情况下可以为完全构造的子对象调用析构函数(15.2)。尾注]

[class.dtor] / 11

如果潜在调用的析构函数被删除或无法从调用上下文访问,则程序格式错误。

没有抛出异常的构造函数没有异常。另请参阅CWG 1915

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章