In the book The C++ Programming Language it is written that you can declare a function to be conditionally noexcept
. For example:
template<typename T>
void my_fct(T& x) noexcept(std::is_pod<T>::value);
noexcept
takes a predicate that must be a constant expression (in the example std::is_pod<T>::value
).
However, in the book it is also written:
The
noexcept()
operator takes an expression as its argument and returnstrue
if the compiler knows that it cannot throw andfalse
otherwise.
Taking this into account, consider:
constexpr bool f() { return true; }
void g() noexcept(f())
{
f();
}
Is g()
marked as noexcept
or not? I see two possibilities:
f()
is evaluated at compile-time because it is marked constexpr
, it returns true
and as a result g()
is marked noexcept
.f()
cannot throw an exception because f()
is not marked noexcept
. As a result g()
is not marked noexcept
.Which one does happen? How can I select one or other behavior for noexcept
?
The language grammar allows only the noexcept
specifier to appear in that position.
Therefore your first point is correct. g()
will be marked noexcept
, because f()
returns true
.
The noexcept
operator must appear in an expression. The noexcept
specifier takes an expression as argument, so if one wants to have a function be noexcept
depending on whether a certain expression is noexcept
, one has to write:
void g() noexcept(noexcept(f()))
{
f();
}
which will behave according to your second point.
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