C ++ 11异常的析构函数允许抛出现在? [英] C++11 Exception's destructor allows to throw now?

查看:814
本文介绍了C ++ 11异常的析构函数允许抛出现在?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何想法为什么
virtual〜exception()throw()是在C ++ 98,
,但
virtual〜exception()在C ++ 11?



设计决策允许C ++ 11抛出类异常?的析构函数?



这里



c ++ 98:

 类别例外{
public:
exception()throw();
exception(const exception&)throw();
exception& operator =(const exception&)throw();
virtual〜exception()throw();
virtual const char * what()const throw();
}

c ++ 11:

 类异常{
public:
exception()noexcept;
exception(const exception&)noexcept;
exception& operator =(const exception&)noexcept;
virtual〜exception();
virtual const char * what()const noexcept;
}


解决方案


什么是设计决策C ++ 11允许抛出类的析构函数?


没有这样的设计决定(幸运!)。在C ++ 11中,默认情况下,即使明确声明的析构函数也被限定为 noexcept 。这可以从C ++ 11标准的第12.4 / 3节中看出:


一个析构函数的声明, (15.4)。




从第15.4 / 14节开始,它规定了隐式声明的异常规范:


继承构造函数(12.9)和隐式声明的特殊成员函数(第12条)有一个
异常规范。如果 f 是继承构造函数或隐式声明的默认构造函数,请复制
constructor,move constructor, destructor ,复制赋值运算符或移动赋值运算符,其隐含的
exception-specification指定type-id T 当且仅当 T 被直接由f的隐式定义调用的函数的异常规范
所允许; 允许所有异常,如果直接的任何函数
调用允许所有异常,并且 f 异常规范 noexcept(true)如果每个函数直接
调用允许没有异常




在上面的两个段落中,保证(给出你引用 exception 的析构函数)的声明,析构函数 exception 不会抛出。



这也在C ++ 11标准的第18.8.1 / 7-8节中明确说明:



< blockquote>

virtual〜exception();



7 em 不会抛出任何异常。 p>

请注意,动态异常规范(例如 throw() C ++ 11。根据附件D的§D.4 / 1:


使用动态异常规范已弃用。



any idea why virtual ~exception() throw() is in C++98, but virtual ~exception() is in C++11?

What's the design decision that allows C++11 to throw in the destructor of the class exception?

From here:

c++98:

class exception {
public:
  exception () throw();
  exception (const exception&) throw();
  exception& operator= (const exception&) throw();
  virtual ~exception() throw();
  virtual const char* what() const throw();
}

c++11:

class exception {
public:
  exception () noexcept;
  exception (const exception&) noexcept;
  exception& operator= (const exception&) noexcept;
  virtual ~exception();
  virtual const char* what() const noexcept;
}

解决方案

What's the desing decision makes C++11 allow to throw in the destructor of the class exception?

There was no such design decision (fortunately!). In C++11, even explicitly declared destructors are qualified as noexcept by default. This can be evinced from paragraph 12.4/3 of the C++11 Standard:

A declaration of a destructor that does not have an exception-specification is implicitly considered to have the same exception-specification as an implicit declaration (15.4).

And from paragraph 15.4/14, which specifies what exception specification an implicit declaration has:

An inheriting constructor (12.9) and an implicitly declared special member function (Clause 12) have an exception-specification. If f is an inheriting constructor or an implicitly declared default constructor, copy constructor, move constructor, destructor, copy assignment operator, or move assignment operator, 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 allows all exceptions if any function it directly invokes allows all exceptions, and f has the exception-specification noexcept(true) if every function it directly invokes allows no exceptions.

Together, the above two paragraphs guarantee (given the declaration you quoted of exception's destructor) that the destructor of exception won't throw.

This is also explicitly stated in paragraphs 18.8.1/7-8 of the C++11 Standard:

virtual ~exception();

7 Effects: Destroys an object of class exception.

8 Remarks: Does not throw any exceptions.

Notice, that dynamic exception specifications (such as throw()) are deprecated in C++11. Per § D.4/1 of the Annex D:

The use of dynamic-exception-specifications is deprecated.

这篇关于C ++ 11异常的析构函数允许抛出现在?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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