How to use the noexcept operator correctly

I implemented a smart pointer that stores an object of type T with a proxy function that calls the internal methods of the object:

template <class Function, class ...Args, class ...Params> inline bool call( Function (T::*function)(Args...) const, Params&& ...args ) const noexcept( noexcept( function )); 

But I found a strange problem - when std :: exception is generated in a member function, the program exits even if the proxy function is called inside the try block. So my question is: is it correct to use the noexcept operator, and if not, how to use it in this case?

+4
source share
1 answer

Per C ++ 11 ยง5.3.7 / 1:

The noexcept operator determines whether evaluating its operand, which is an unvalued operand (section 5), can throw an exception (15.1).

Evaluation of an expression (function) cannot throw an exception, so noexcept(function) evaluates to true . Note that this is not the same as evaluating an expression (*function)(std::forward<Params>(args)...) , noexcept((*function)(std::forward<Params>(args)...)) , of course, will be evaluated using false , since a member function pointer is not a qualified noexcept .

noexcept is the qualification of types of function pointers such as const . Since the pointer type of the call function accepts NOT noexcept -qualified, complex noexcept(noexcept(...)) will always be evaluated as noexcept(false) .

EDITOR: The following is incorrect, it is impossible to overload only on the basis of noexcept qualification of the function pointer, since "The specification of the exception is not considered part of the type of functions". (ยง15.4 / 13)

If you want call be noexcept , if the function pointer is set to noexcept -qualified member function, you need to provide call(R (T::*)() const noexcept, ...) overload call(R (T::*)() const noexcept, ...) .

+4
source

Source: https://habr.com/ru/post/1496185/


All Articles