Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ omitting `noexcept` specifier versus `noexcept(false)`, what is their precise meaning?

If I mark a function as noexcept(false), or any other expression which evaluates to false, what does it means? (1) am I ensuring to the compiler that the function can throw an exception?, (2) or am I ensuring nothing about whether it can throw exceptions or not?

And lastly, if I omit the noexcept specifier, it is equivalent to noexcept(false), or only equivalent to the (2)nd meaning stated above?

like image 428
Peregring-lk Avatar asked Apr 21 '15 15:04

Peregring-lk


People also ask

What does it mean to specify a function as Noexcept false )?

The noexcept operator performs a compile-time check that returns true if an expression is declared to not throw any exceptions. It can be used within a function template's noexcept specifier to declare that the function will throw exceptions for some types but not others.

What is Noexcept false?

The noexcept specification is equivalent to the noexcept(true) specification. throw() is equivalent to noexcept(true) but was deprecated with C++11 and will be removed with C++20. In contrast, noexcept(false) means that the function may throw an exception.

What does Noexcept mean in C ++?

A noexcept-expression is a kind of exception specification: a suffix to a function declaration that represents a set of types that might be matched by an exception handler for any exception that exits a function.

What happens when Noexcept function throws?

Note that noexcept doesn't actually prevent the function from throwing exceptions or calling other functions that are potentially throwing. Rather, when an exception is thrown, if an exception exits a noexcept function, std::terminate will be called.


2 Answers

By specifying noexcept(true), you claim that the function never throws exceptions. By specifying noexcept(false), or not specifying anything, you do not claim that the function never throws exceptions.

So it's basically your statement (2), but note that for the compiler, that's equivalent to your statement (1). If the compiler is not assured that the function will not throw, it must assume that it can.

The relevant bit of the standard is C++11 15.4/12:

A function with no exception-specification or with an exception-specification of the form noexcept(constant-expression) where the constant-expression yields false allows all exceptions. An exception-specification is non-throwing if it is of the form throw(), noexcept, or noexcept(constant-expression) where the constant-expression yields true. A function with a non-throwing exception-specification does not allow any exceptions.

There are only two deviations from that rule. One is destructors—putting no exception specification on a destructor gives the destructor the same exception specification as the default-generated one would have. That is, noexcept(true) if and only if all functions which would be directly invoked from the default-generated destructor are noexcept(true).

The other one are deallocation functions (operator delete)—a deallocation function without an explicit exception specification is treated as noexcept(true).

like image 176
Angew is no longer proud of SO Avatar answered Oct 01 '22 09:10

Angew is no longer proud of SO


Omitting the noexcept specifier is equivalent to noexcept(false), except for destructors, where omitting the specifier means letting the compiler deduce from the members and base classes.

like image 32
Sebastian Redl Avatar answered Oct 01 '22 08:10

Sebastian Redl