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?
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.
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.
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.
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.
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 yieldsfalse
allows all exceptions. An exception-specification is non-throwing if it is of the formthrow()
,noexcept
, ornoexcept(constant-expression)
where the constant-expression yieldstrue
. 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)
.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With