隐式使用析构函数 [英] Implicit use of destructor

查看:193
本文介绍了隐式使用析构函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a class with a deleted destructor (in practice, it needs outside help to be destroyed):

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

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

When I try to use its move constructor, the compiler complains:

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;

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

What's going on? there are no other members that could throw, and neither does the constructor body, so why is there any cause for the move constructor to invoke the destructor?

推荐答案

[class.base.init] / 12


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

In a non-delegating constructor, the destructor for each direct or virtual base class and for each non-static data member of class type is potentially invoked (12.4). [ Note: This provision ensures that destructors can be called for fully-constructed sub-objects in case an exception is thrown (15.2). —end note ]

[class.dtor] / 11


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

A program is ill-formed if a destructor that is potentially invoked is deleted or not accessible from the context of the invocation.

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

No exception is made for a constructor that doesn't throw. See also CWG 1915.

这篇关于隐式使用析构函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆