What is the difference between this:
bool foo(int){return 0;}
void bar()noexcept(foo){}
And
void bar()noexcept(foo(10)){}
The difference isn't very important since both are ill-formed. But, let's look at why that is:
First is ill-formed because when foo
is contextually convertd to bool
, it will first implicitly convert to a pointer to function. Although the pointer is always going to point fo foo
and thus the result is always true, such conversion is nevertheless not a constant expression and is thus not allowed in the noexcept
specification.
Second is ill-formed because foo
is not a constexpr function, so it may not be called in a noexcept
specification.
But what about the second version noexcept(noexcept(foo(10)))
?
noexcept(foo(10))
is false if the function invoked by the expression foo(10)
is potentially throwing, and true if it is noexcept. Thus, this achieves the "copying" of the exception specification. Nearly always this is done when the expression in question is used within the function:
void bar() noexcept(noexcept(foo(10))) {
bool example = foo(10);
}