简体   繁体   English

C ++:noexcept(或throw())virtual destructor = default;

[英]C++ : noexcept (or throw()) virtual destructor = default;

The following code is legal? 以下代码合法?

class C
{
    virtual ~C() noexcept = default;
};

or 要么

class C
{
    virtual ~C() throw() = default;
};

(throw() is deprecated, but my compiler doesn't support noexcept ;;) (throw()已弃用,但我的编译器不支持noexcept ;;)

8.4.2 [dcl.fct.def.default] An explicitly-defaulted function [...] may have an explicit exception-specification only if it is compatible (15.4) with the exception-specification on the implicit declaration. 8.4.2 [dcl.fct.def.default]明确默认的函数[...]只有在与隐式声明中的异常规范兼容(15.4)时才有明确的异常规范。

15.4/3 [except.spec] Two exception-specifications are compatible if: 15.4 / 3 [except.spec]如果符合以下条件,则两个异常规范兼容:

  • both are non-throwing (see below), regardless of their form, 两者都是非投掷的(见下文),无论其形式如何,
  • both have the form noexcept(constant-expression) and the constant-expressions are equivalent, or 两者都有noexcept(constant-expression)形式,而constant-expression是等价的,或者
  • both are dynamic-exception-specifications that have the same set of adjusted types. 两者都是具有相同调整类型集的动态异常规范。

So you can only give an explicit exception-specification if it exactly matches the one that the implicit declaration of the destructor would have. 因此,如果它与析构函数的隐式声明所具有的完全匹配,则只能提供显式异常规范。

The exception-specification that the implicit destructor would have depends on the functions it would call: 隐式析构函数具有的异常规范取决于它将调用的函数:

15.4/14 [except.spec] An implicitly declared special member function shall have an exception-specification. 15.4 / 14 [except.spec]隐式声明的特殊成员函数应具有异常规范。 If f is an implicitly declared [...] destructor, [...] its implicit exception-specification specifies the type-id T if and only if T is allowed by the exception-specification of a function directly invoked by f's implicit definition; 如果f是一个隐式声明的析构函数,它的隐式异常规范指定了type-id T当且仅当T被f的隐式定义直接调用的函数的异常指定所允许时; f shall allow all exceptions if any function it directly invokes allows all exceptions, and f shall allow no exceptions if every function it directly invokes allows no exceptions. 如果它直接调用的任何函数允许所有异常,则f应允许所有异常,如果它直接调用的每个函数都不允许异常,则f不允许异常。

The functions that a destructor calls are the destructors of the class's non-static data members, its base classes, and its virtual base classes. 析构函数调用的函数是类的非静态数据成员,其基类和虚拟基类的析构函数。

In your case, since the class has no data members and no base classes and therefore it calls no functions, it falls into the final case. 在您的情况下,由于该类没有数据成员且没有基类,因此它不调用任何函数,因此它属于最终情况。 Every function it directly invokes (there are none) allows no exceptions, so this destructor must allow no exceptions. 它直接调用的每个函数(没有)都不允许异常,所以这个析构函数必须不允许异常。 Therefore, your exception-specification has to be non-throwing, so nothrow , except() , and exception(constant expression that yields true) are the only appropriate exception-specifications you can give, so your code is fine. 因此,你的异常规范必须是非抛出的,所以不要nothrowexcept()exception(constant expression that yields true)是你能给出的唯一合适的异常规范,所以你的代码很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM