Difference between C ++ 03 throw () specifier C ++ 11 noexcept

Is there any other difference between throw() and noexcept from checking runtime and compile time respectively?

The Wikipedia C ++ 11 article assumes that C ++ 03 throw specs are out of date.
Why so, noexcept is able to sufficiently cover everything at compile time?

[Note: I also called this question in this article , but I couldn’t get a solid reason for obsolescence.]

+75
c ++ exception throw c ++ 11 noexcept
Oct 11
source share
3 answers

Exception specifiers were deprecated, because exception specifiers were usually a terrible idea . noexcept was added because this is one useful use of the exception specifier: knowing when the function will not throw an exception. Thus, it becomes a binary choice: functions that will throw, and functions that will not be thrown.

noexcept , not just removing all throw specifiers other than throw() , because noexcept more powerful. noexcept may have a parameter that compile time resolves to a boolean. If the boolean value is true, then noexcept is stored. If the boolean value is false, then noexcept does not get up, and the function may discard.

So you can do something like this:

 struct<typename T> { void CreateOtherClass() { T t{}; } }; 

Does CreateOtherClass exceptions? This may be if the default T constructor can. How do we talk? Like this:

 struct<typename T> { void CreateOtherClass() noexcept(is_nothrow_default_constructible<T>::value) { T t{}; } }; 

Thus, CreateOtherClass() will throw an iff, which generates a default constructor of this type. This fixes one of the main issues with exception qualifiers: their inability to propagate the call stack.

You cannot do this with throw() .

+98
Oct 11 '12 at 6:23
source share

noexcept not checked at compile time.

An implementation should not reject an expression just because, when executed, it throws or can throw an exception that the containing function does not allow.

When a function declared by noexcept or throw() tries to throw an exception, the only difference is that you call terminate and othe calls unexpected , and the last exception handling style is effectively deprecated.

+28
Oct 11 '12 at 6:23
source share

std :: unexpected () is called by the C ++ runtime when the dynamic specification of the exception is violated: the exception is selected from a function whose exception specification prohibits exceptions of this type.

std :: unexpected () can also be called directly from the program.

In any case, std :: unexpectedly calls the currently installed std :: unexpected_handler. By default, std :: unexpected_handler calls std :: terminate.

+1
Jul 10 '17 at 17:08
source share



All Articles