cuda::experimental::stf::on_throw#
-
template<class _Reaction>
auto cuda::experimental::stf::on_throw( - _Reaction &&__reaction,
- const ::cuda::std::source_location __loc = ::cuda::std::source_location::current()
Creates a policy saying how to react if a callable throws.
Apply the policy with
on_throw(reaction) << callable, which evaluates to the result of the callable when nothing goes wrong. The reaction is one of four things, recognized by type:A handler: any
noexceptcallable accepting aconst std::exception*and acuda::std::source_location, be it a function, a function pointer, or a function object, capturing or not. It is invoked with the caught exception, or withnullptrfor an exception that does not derive fromstd::exception, along with__loc. Its return type says what happens next: returningdecltype(::std::ignore)resumes execution with a default-constructed result, and returningvoidclaims the handler ends the program, withstd::abortrunning right after it in case it does not.notifyis such a handler.A terminating action: anything convertible to
void (*)(),std::abortandstd::terminatebeing the obvious ones. The exception is reported throughnotify, the action runs, andstd::abortfollows in case it returns.std::ignore, which suppresses the exception silently and resumes execution with a default-constructed result.Anything else, taken as a replacement value for the result. It must be convertible to the callable’s result type, and is moved into the result if the policy owns it.
The two resuming reactions leave a
voidresult alone and require a default-constructible type of a non-void one, having nothing to refer to for a reference.The callable itself must not be
noexcept: an exception raised inside one ends the program where it stands, leaving the reaction unreachable, so such a pairing is rejected instead of standing there looking like protection. Call such a callable directly.A callable returning a reference therefore goes with a terminating action, which never has to produce a result, or with a replacement passed as an lvalue of the same type, which the policy refers to rather than copies and which the caller keeps alive:
int fallback = 42; int& x = on_throw(fallback) << [] { return returns_a_reference(); }; // x is fallback on a throw
- Parameters:
__reaction – [in] The reaction, which the policy owns if passed an rvalue and refers to if passed an lvalue.
__loc – [in] The location passed to the handler; defaults to the call site.
- Returns:
A policy object consumed by
operator<<.