In the following code, I thought that the assertion shouldn't fire but it does.
struct A
{
~A() noexcept(false);
};
A f() noexcept;
int main()
{
static_assert(noexcept(f()), "f must be noexcept");
}
The function f()
is noexcept obviously, but noexcept(f())
is evaluated to false. (in both of gcc and clang)
Am i missing something or is it a bug?
An implicit declaration of a destructor is considered to be noexcept(true) according to [except. spec], paragraph 14. As such, destructors must not be declared noexcept(false) but may instead rely on the implicit noexcept(true) or declare noexcept explicitly.
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.
In general, you should use noexcept when you think it will actually be useful to do so. Some code will take different paths if is_nothrow_constructible is true for that type. If you're using code that will do that, then feel free to noexcept appropriate constructors.
Theoretically speaking, noexcept would improve performance. But it might also cause some problems on the other hand. In most of cases, it shouldn't be specified because the pros are too few to be considered and it might make your code upgrading painful.
The noexcept
operator on an expression e
tells you whether the set of potential exceptions of the expression is empty. This set contains the potential exceptions of the destructor, as per [except.spec]/(13.2):
If
e
implicitly invokes one or more functions (such as an overloaded operator, an allocation function in a new-expression, or a destructor if e is a full-expression (1.9)), S is the union of: [...] the sets of types in the exception specifications of all such functions
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