std::execution::completion_signatures
| Defined in header <execution>
|
||
template< /*completion-signature*/... Fns >
struct completion_signatures { /* unspecified */ };
|
(since C++26) | |
The class template completion_signatures lists the ways in which a sender can complete, its completion signatures. Each type in Fns is a function type of one of these forms:
execution::set_value_t(Vs...): the sender can complete withexecution::set_value(rcvr, vs...), with arguments of the typesVs...;execution::set_error_t(E): the sender can complete withexecution::set_error(rcvr, e), with an argument of the typeE;execution::set_stopped_t(): the sender can complete withexecution::set_stopped(rcvr).
Each of Vs... and E is an object type or a reference type. The program is ill-formed if any type in Fns has another form.
A sender type declares its completion signatures with a static member function template that returns a completion_signatures object:
template<class Self, class... Env>
static consteval auto get_completion_signatures()
{
return execution::completion_signatures</* signatures */>();
}
Self is the type of the sender, with the cv-qualifiers and value category in which it is used, and Env... is either empty or the type of the environment of the receiver. A sender whose completion signatures depend on the environment declares template<class Self, class Env> instead; see dependent_sender. If the sender cannot be used, for example in a given environment, the function can throw an exception: the program is then ill-formed, and compilers show the exception in the diagnostic.
get_completion_signatures obtains the completion signatures of a sender, and value_types_of_t, error_types_of_t and sends_stopped extract information from them.
Notes
- The order of the signatures has no meaning, but specializations that list the same signatures in a different order are different types.
- A sender must not complete in a way that its completion signatures do not list. A sender that can fail with an exception, for example because its operation calls a function that can throw, lists
execution::set_error_t(std::exception_ptr). - Early drafts of C++26 let a sender declare its completion signatures with a member type alias
using completion_signatures = execution::completion_signatures<...>;, or with a non-static member functionget_completion_signaturesthat took the environment as an argument. Some implementations still accept these forms.
Example
A sender that declares its completion signatures, and a function that prints the completion signatures of a sender. The order of the signatures may differ between implementations.
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 <exception>
#include <execution>
#include <print>
#include <utility>
namespace ex = std::execution;
// Completes with its value if the value is positive, and as stopped otherwise
struct positive_or_stopped
{
using sender_concept = ex::sender_tag;
int value;
template<class Self, class... Env>
static consteval auto get_completion_signatures()
{
return ex::completion_signatures<ex::set_value_t(int),
ex::set_stopped_t()>();
}
template<class Rcvr>
struct op_state
{
using operation_state_concept = ex::operation_state_tag;
Rcvr rcvr;
int value;
void start() & noexcept
{
if (value > 0)
ex::set_value(std::move(rcvr), value);
else
ex::set_stopped(std::move(rcvr));
}
};
template<class Rcvr>
op_state<Rcvr> connect(Rcvr rcvr) const
{
return {std::move(rcvr), value};
}
};
// Prints each signature in a completion_signatures specialization
template<class... Ts>
void print_sig(ex::set_value_t (*)(Ts...))
{
std::println(" set_value with {} value(s)", sizeof...(Ts));
}
void print_sig(ex::set_error_t (*)(std::exception_ptr))
{
std::println(" set_error with std::exception_ptr");
}
void print_sig(ex::set_stopped_t (*)()) { std::println(" set_stopped"); }
template<class... Sigs>
void print_sigs(const char* name, ex::completion_signatures<Sigs...>)
{
std::println("{}:", name);
(print_sig(static_cast<Sigs*>(nullptr)), ...);
}
int main()
{
print_sigs("positive_or_stopped",
ex::get_completion_signatures<positive_or_stopped>());
// then replaces the value completion, and adds set_error because
// the function may throw
auto twice = positive_or_stopped{21} | ex::then([](int i)
{
return 2 * i;
});
print_sigs("then", ex::get_completion_signatures<decltype(twice)>());
auto [v] = std::this_thread::sync_wait(twice).value();
std::println("Value: {}", v);
if (!std::this_thread::sync_wait(positive_or_stopped{0}))
std::println("Stopped");
}
Output:
positive_or_stopped:
set_value with 1 value(s)
set_stopped
then:
set_value with 1 value(s)
set_error with std::exception_ptr
set_stopped
Value: 42
Stopped
See also
| obtains the completion signatures of a sender, or their type (function) | |
(C++26) |
obtains the value completion type of a sender (alias template) |
(C++26) |
obtains the error completion type of a sender (alias template) |
(C++26) |
determines whether the sender supports stopped completion (variable template) |
(C++26) |
specifies that the ways a sender can complete are known, for a given environment or without one (concept) |
(C++26) |
specifies that the ways a sender can complete depend on the environment it is started in (concept) |