Namespaces
Variants

std::execution::default_domain

From cppreference.com
 
 
Execution control library
Concepts
 
Defined in header <execution>
struct default_domain
{
    template< class Tag, execution::sender Sndr, /*queryable*/ Env >
    static constexpr decltype(auto)
        transform_sender( Tag, Sndr&& sndr, const Env& env )
            noexcept(/* see below */);

    template< class Tag, execution::sender Sndr, class... Args >
    static constexpr decltype(auto)
        apply_sender( Tag, Sndr&& sndr, Args&&... args )
            noexcept(/* see below */);
};
(since C++26)

default_domain is the execution domain of the implementations of the sender algorithms that the standard library provides. It stands for work that runs on ordinary threads of execution, such as the thread that calls this_thread::sync_wait or the thread that drives a run_loop.

A domain is a class whose member function transform_sender can replace a sender, when it is connected to a receiver, with another sender that does the same work in a way suited to a particular execution resource. default_domain is used wherever no other domain applies:

  • get_domain returns it for an environment that has neither a domain nor a scheduler;
  • get_completion_domain returns it for a scheduler that does not report a domain of its own;
  • transform_sender and apply_sender use it when the domain they were given does not have a suitable member function transform_sender or apply_sender.

A domain written by the program therefore needs to provide only the transformations that it changes.

Member functions

execution::default_domain::transform_sender

template< class Tag, execution::sender Sndr, /*queryable*/ Env >
static constexpr decltype(auto)
    transform_sender( Tag, Sndr&& sndr, const Env& env )
        noexcept(/* see below */);
(since C++26)

Applies the transformation that the algorithm which made sndr specifies for itself. Given T, the type execution::tag_of_t<Sndr>:

  • if T().transform_sender(Tag(), std::forward<Sndr>(sndr), env) is well-formed, returns its result;
  • otherwise, returns sndr itself: an lvalue as a reference, and an rvalue moved into the returned object.

The function is noexcept if the selected expression is.

Tag is execution::set_value_t when the domain is the one in which the sender completes, and execution::start_t when it is the one in which the sender starts; see transform_sender. Several sender algorithms of the standard library are specified in terms of simpler ones, and are replaced by them here when given execution::set_value_t, for example:

  • bulk(sndr, policy, shape, f) becomes bulk_chunked(sndr, policy, shape, f2), where f2 calls f for each index of the chunk it is given;
  • starts_on(sch, sndr) becomes a sender that moves to sch and then starts sndr, made with let_value and continues_on;
  • on(...) becomes a combination of starts_on and continues_on;
  • when_all_with_variant(sndrs...) becomes when_all(into_variant(sndrs)...);
  • stopped_as_optional(sndr) becomes a then that makes an std::optional from the value, followed by a let_stopped that sends an empty one;
  • stopped_as_error(sndr, err) becomes a let_stopped that sends just_error(err).

The senders of the other algorithms, and senders that are not made by the standard library, are returned unchanged.

execution::default_domain::apply_sender

template< class Tag, execution::sender Sndr, class... Args >
static constexpr decltype(auto)
    apply_sender( Tag, Sndr&& sndr, Args&&... args )
        noexcept(/* see below */);
(since C++26)

Calls the default implementation of the algorithm Tag, which consumes sndr instead of returning a new sender: returns Tag().apply_sender(std::forward<Sndr>(sndr), std::forward<Args>(args)...). This overload participates in overload resolution only if that expression is well-formed. It is noexcept if that expression is.

In the standard library, this_thread::sync_wait and this_thread::sync_wait_with_variant work this way: they call apply_sender with the domain in which the sender completes. For default_domain, this connects the sender to a receiver, starts it, and runs a run_loop on the calling thread until the operation completes.

Notes

  • A domain does not need to derive from default_domain, since transform_sender and apply_sender fall back to default_domain for anything the domain does not handle. A transform_sender of a domain can also call execution::default_domain().transform_sender(tag, sndr, env) itself, to apply the library's transformation to a sender after inspecting it, or to the senders that it does not replace.
  • The library's transformations happen when the sender is connected, not when the algorithm is called: execution::bulk(sndr, policy, shape, f) returns a bulk sender. When a transformation returns a sender of another type, transform_sender gives the new sender to the domain in which it completes, so a domain is first given the bulk sender and then the bulk_chunked sender that default_domain made from it. A domain that replaces bulk_chunked and bulk_unchunked senders therefore also handles bulk, unless it replaces bulk senders itself.
  • The transform_sender of indeterminate_domain, the domain of a sender that can complete in one of several domains, returns what the transform_sender of default_domain returns.
  • In the original design of std::execution, transform_sender of a domain had no Tag parameter, and default_domain also had a member function transform_env. P3826R5 added the parameter and removed transform_env before C++26 was published. Older implementations and articles may show the earlier interface.

Example

logging_domain reports each sender that it is given, and then lets default_domain transform it: the bulk sender is replaced with a bulk_chunked sender, and the other senders are returned unchanged. The receiver's environment makes logging_domain the domain in which the work starts, and since just completes where it starts, every sender of the chain completes in logging_domain too.
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 <concepts>
#include <exception>
#include <execution>
#include <print>
#include <string_view>
#include <type_traits>
#include <utility>

namespace ex = std::execution;

// The name of the algorithm that made a sender
template<class Sndr>
constexpr std::string_view algorithm()
{
    using Tag = ex::tag_of_t<std::remove_cvref_t<Sndr>>;
    if constexpr (std::same_as<Tag, ex::just_t>)
        return "just";
    else if constexpr (std::same_as<Tag, ex::then_t>)
        return "then";
    else if constexpr (std::same_as<Tag, ex::bulk_t>)
        return "bulk";
    else if constexpr (std::same_as<Tag, ex::bulk_chunked_t>)
        return "bulk_chunked";
    else
        return "other";
}

// A domain that reports each sender it is given, and then lets
// default_domain transform it
struct logging_domain
{
    template<ex::sender Sndr, class Env>
    static decltype(auto) transform_sender(ex::set_value_t, Sndr&& sndr,
                                           const Env& env)
    {
        std::println("Transforming a {} sender", algorithm<Sndr>());
        return ex::default_domain().transform_sender(
            ex::set_value, std::forward<Sndr>(sndr), env);
    }
};

// A receiver whose environment says that the work starts in logging_domain
struct print_result
{
    using receiver_concept = ex::receiver_tag;

    void set_value(int n) noexcept { std::println("Result: {}", n); }
    void set_error(std::exception_ptr) noexcept {}

    auto get_env() const noexcept
    {
        return ex::prop(ex::get_domain, logging_domain());
    }
};

int main()
{
    auto sndr = ex::just(10)
              | ex::bulk(ex::seq, 3, [](int i, int& n) { n += i; })
              | ex::then([](int n) { return n * 2; });

    // connect passes each sender of the chain to transform_sender
    auto op = ex::connect(std::move(sndr), print_result());
    ex::start(op);
}

Output:

Transforming a then sender
Transforming a bulk sender
Transforming a bulk_chunked sender
Transforming a just sender
Result: 26

See also

asks a queryable object for its associated execution domain tag
(customization point object)[edit]
asks the completion domain associated with a completion tag from a sender's attributes
(customization point object)[edit]
execution domain of work that can complete in one of several domains, using the default implementations of the algorithms
(class template) [edit]
transforms into a new sender under a given execution domain tag
(function template) [edit]
runs an algorithm that consumes a sender with the implementation of a given execution domain
(function template) [edit]
obtains the tag type of a sender
(alias template)[edit]