std::execution::error_types_of_t
| Defined in header <execution>
|
||
template< class Sndr, class Env = execution::env<>,
template< class... > class Variant = /*variant-or-empty*/ >
requires execution::sender_in<Sndr, Env>
using error_types_of_t = /* see below */;
|
(since C++26) | |
Names the types of the errors with which a sender of type Sndr can complete when it is connected to a receiver whose environment has type Env.
Each error completion signature execution::set_error_t(E) in execution::completion_signatures_of_t<Sndr, Env> has exactly one argument type. Variant is applied to all of them: a sender with the error completions execution::set_error_t(E1) and execution::set_error_t(E2) gives Variant<E1, E2>. Variant does not have to be a variadic template or a variant, but it must accept the arguments it gets: for example, std::type_identity_t works when the sender has exactly one error completion.
By default, /*variant-or-empty*/<Ts...> is std::variant<Us...>, where Us... are the types std::decay_t<Ts>... with duplicates removed; if there are no Ts, it is an unspecified class type that cannot be constructed.
Sndr includes the value category with which the sender is going to be used: Sndr& for an lvalue, Sndr or Sndr&& for an rvalue.
Notes
- Many senders complete with
execution::set_error_t(std::exception_ptr)when a function they call can throw: for example, then with a function that is notnoexcept. A sender with no error completions givesVariant<>, soexecution::error_types_of_t<Sndr, Env, std::variant>isstd::variant<>for it. - Generic code should not depend on the order of the alternatives in the
Variant. - The default environment
execution::env<>is the empty environment, not the absence of one. To know the error types of a sender connected to a receiver of typeRcvr, useexecution::error_types_of_t<Sndr, execution::env_of_t<Rcvr>>. - Early proposals of
std::executionprovided this type through a member alias templateerror_types<Variant>of a sender traits class. Older implementations and articles may show the earlier interface.
Example
Keeps the error with which a sender completed in a std::optional of its error_types_of_t, and prints a message for it.
The execution control library components may not be available in the online compilers yet, so this link runs the example on Compiler Explorer with stdexec (one of the reference implementations).
#include <charconv>
#include <concepts>
#include <exception>
#include <execution>
#include <optional>
#include <print>
#include <stdexcept>
#include <string>
#include <string_view>
#include <system_error>
#include <thread>
#include <variant>
namespace ex = std::execution;
std::string message(std::exception_ptr e)
{
try { std::rethrow_exception(e); }
catch (const std::exception& x) { return x.what(); }
}
std::string message(std::error_code ec) { return ec.message(); }
int to_int(std::string_view s)
{
int v = 0;
if (std::from_chars(s.data(), s.data() + s.size(), v).ec != std::errc{})
throw std::invalid_argument("not a number");
return v;
}
using parse_sender = decltype(ex::just(std::string_view()) | ex::then(to_int));
static_assert(std::same_as<ex::error_types_of_t<parse_sender>,
std::variant<std::exception_ptr>>);
static_assert(std::same_as<ex::error_types_of_t<decltype(ex::just(1)),
ex::env<>, std::variant>,
std::variant<>>);
// Runs a sender of an int that can fail, and keeps the error, if any
template<ex::sender Sndr>
void report(Sndr sndr)
{
std::optional<ex::error_types_of_t<Sndr>> error;
auto [v] = std::this_thread::sync_wait(
std::move(sndr) | ex::upon_error([&](auto e)
{
error.emplace(e);
return 0;
})).value();
if (error)
std::visit([](auto& e) { std::println("error: {}", message(e)); },
*error);
else
std::println("{}", v);
}
int main()
{
report(ex::just_error(std::make_error_code(std::errc::timed_out)));
report(ex::just(std::string_view("42")) | ex::then(to_int));
report(ex::just(std::string_view("x42")) | ex::then(to_int));
}
Possible output:
error: Connection timed out
42
error: not a number
See also
(C++26) |
obtains the value completion type of a sender (alias template) |
(C++26) |
determines whether the sender supports stopped completion (variable template) |
| lists the ways a sender can complete (class template) | |
| obtains the completion signatures of a sender, or their type (function) | |
(C++26) |
error completion function indicating that an error occurred during calculation or scheduling (customization point object) |
(C++26) |
chains a function onto a sender, calling it with the error if the sender fails and sending its result on as a value (customization point object) |
(C++26) |
chains a function onto a sender, calling it with the error if the sender fails and continuing with the sender it returns (customization point object) |