Reputation: 3569
I've implemented a smart pointer that stores an object of type T with proxy function that calls the internal object's methods:
template <class Function, class ...Args, class ...Params>
inline bool call( Function (T::*function)(Args...) const, Params&& ...args ) const noexcept( noexcept( function ));
But I have found a strange problem - when an std::exception is generated in a member function, the program is terminated, even though the proxy function is called within a try block. So my question is: is it a correct way to use noexcept operator, and if not, how should I use it in this case?
Upvotes: 4
Views: 2063
Reputation: 42544
Per C++11 §5.3.7/1:
The
noexcept
operator determines whether the evaluation of its operand, which is an unevaluated operand (Clause 5), can throw an exception (15.1).
Evaluating the expression (function)
cannot throw an exception, so noexcept(function)
evaluates to true
. Note that this is not the same as evaluating the expression (*function)(std::forward<Params>(args)...)
, noexcept((*function)(std::forward<Params>(args)...))
would certainly evaluate to false
, since the member function pointer is not qualified noexcept
.
noexcept
is a qualification on function pointer types like const
. Since the function pointer type call
accepts is NOT noexcept
-qualified, that complicated noexcept(noexcept(...))
will always evaluate as noexcept(false)
.
EDIT: The below is incorrect, it's not possible to overload purely on the basis of noexcept
qualification of a function pointer since "An exception-specification is not considered part of a function’s type." (§15.4/13)
If you want call
to be noexcept
when given a noexcept
-qualified member function pointer, you need to provide an overload call(R (T::*)() const noexcept, ...)
.
Upvotes: 4