std::execution::apply_sender
| Defined in header <execution>
|
||
template< class Domain, class Tag, execution::sender Sndr, class... Args >
constexpr decltype(auto)
apply_sender( Domain dom, Tag, Sndr&& sndr, Args&&... args )
noexcept(/* see below */);
|
(since C++26) | |
Runs the algorithm Tag on the sender sndr with the implementation that the execution domain dom provides. Returns dom.apply_sender(Tag(), std::forward<Sndr>(sndr), std::forward<Args>(args)...) if that expression is well-formed, and otherwise execution::default_domain().apply_sender(Tag(), std::forward<Sndr>(sndr), std::forward<Args>(args)...), which calls the implementation of the standard library; see default_domain. This overload participates in overload resolution only if the selected expression is well-formed.
transform_sender lets a domain replace a sender before it is connected. apply_sender is for the algorithms that do not make a new sender, but connect and start the sender they are given themselves, and return something else: the sender consumers. In the standard library, these are this_thread::sync_wait and this_thread::sync_wait_with_variant. std::this_thread::sync_wait(sndr) calls apply_sender(D(), std::this_thread::sync_wait, sndr), where D is the domain in which sndr completes, execution::get_completion_domain<set_value_t>(execution::get_env(sndr), env), and env is the environment of the receiver that sync_wait connects the sender to. A domain for an execution resource that has its own way of waiting for work can therefore provide its own sync_wait.
Parameters
| dom | - | the domain whose implementation of the algorithm is used |
| Tag | - | the type of the algorithm, such as std::this_thread::sync_wait_t
|
| sndr | - | the sender that the algorithm consumes |
| args | - | the other arguments of the algorithm |
Return value
The result of the selected expression.
Exceptions
Throws any exception thrown by the selected expression. It is noexcept if that expression is.
Notes
- Unlike transform_sender,
apply_senderdoes not find the domain itself: the caller passes it. Each sender consumer specifies which domain it uses. - A domain's
apply_senderforstd::this_thread::sync_wait_tmust return the same type as the implementation of the standard library, a std::optional of a std::tuple of the values; otherwise the call tosync_waitis ill-formed. It must also block the calling thread until the work completes, assync_waitdoes, or the behavior is undefined. It can callexecution::default_domain().apply_sender(std::this_thread::sync_wait, sndr)to wait as the library does, for example after preparing the execution resource. - The implementation of
sync_wait_with_variantin the standard library callssync_wait(execution::into_variant(sndr)), and the into_variant sender completes in the same domain assndr, so a domain that provides only its ownsync_waitis also used bysync_wait_with_variant. - In the original design of
std::execution,sync_waitcalledapply_senderwith the domain that the attributes ofsndrreported through get_domain, or the domain of its completion scheduler. P3826R5 changed it to the domain in which the sender completes before C++26 was published. Older implementations and articles may show the earlier rule.
Example
accel_domain provides its own sync_wait, which reports the call and then waits as the library does. sync_wait uses it for a sender that completes in accel_domain, and uses the implementation of the standard library for the other sender.
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 <execution>
#include <print>
#include <utility>
namespace ex = std::execution;
// The domain of an execution resource with its own way of waiting for work.
// This one only reports the call, and then waits as the library does
struct accel_domain
{
template<ex::sender Sndr>
static auto apply_sender(std::this_thread::sync_wait_t, Sndr&& sndr)
{
std::println("accel_domain waits for the work");
return ex::default_domain().apply_sender(std::this_thread::sync_wait,
std::forward<Sndr>(sndr));
}
};
// A sender that reports accel_domain as the domain in which it completes
template<ex::sender Sndr>
struct on_accel
{
using sender_concept = ex::sender_tag;
Sndr sndr;
template<class Self, class... Env>
static consteval auto get_completion_signatures()
{
return ex::get_completion_signatures<Sndr, Env...>();
}
auto connect(ex::receiver auto rcvr) &&
{
return ex::connect(std::move(sndr), std::move(rcvr));
}
auto get_env() const noexcept
{
return ex::prop(ex::get_completion_domain<ex::set_value_t>,
accel_domain());
}
};
int main()
{
auto work = ex::just(20) | ex::then([](int n) { return n + 1; });
// Completes in default_domain: sync_wait waits as the library does
auto [a] = std::this_thread::sync_wait(work).value();
std::println("Result: {}", a);
// Completes in accel_domain: sync_wait calls its apply_sender
auto [b] = std::this_thread::sync_wait(on_accel{work}).value();
std::println("Result: {}", b);
// apply_sender can also be called directly with a domain
auto [c] = ex::apply_sender(accel_domain(), std::this_thread::sync_wait,
work).value();
std::println("Result: {}", c);
}
Output:
Result: 21
accel_domain waits for the work
Result: 21
accel_domain waits for the work
Result: 21
See also
(C++26) |
transforms into a new sender under a given execution domain tag (function template) |
(C++26) |
execution domain of the standard library's own implementations of the sender algorithms (class) |
| asks the completion domain associated with a completion tag from a sender's attributes (customization point object) | |
(C++26) |
blocks current thread until the specified sender completes and returns its async result (customization point object) |
| blocks current thread until the specified sender with possibly multiple completion signatures completes and returns its async result (customization point object) | |
(C++26) |
obtains the tag type of a sender (alias template) |