Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
372 views
in Technique[技术] by (71.8m points)

c++ - C++11 Exception's destructor allows to throw now?

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;
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...