Namespaces
Variants

std::execution::indeterminate_domain

From cppreference.com
 
 
Execution control library
Concepts
 
Defined in header <execution>
template< class... Domains >
struct indeterminate_domain
{
    indeterminate_domain() = default;
    constexpr indeterminate_domain( auto&& ) noexcept {}

    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)

indeterminate_domain<Domains...> is the execution domain of work that completes in one of the domains Domains..., when it is not known in advance which one. For example, when_all completes where the child sender that finishes last completes, so if its children complete in different domains, get_completion_domain reports an indeterminate_domain for it.

A domain can replace the senders of some algorithms with senders suited to its execution resource; see get_domain. Most domains replace only a few algorithms, so the library can usually still pick an implementation for work that completes in an indeterminate_domain: it uses the implementation of the standard library, as default_domain does, as long as none of Domains... would replace the sender with another one. The program is ill-formed if one of them would.

When a sender can complete in one of several domains, the library uses their common type as its completion domain, if they have one, and an indeterminate_domain of all of them otherwise. Each domain appears in Domains... only once.

Member functions

execution::indeterminate_domain::indeterminate_domain

indeterminate_domain() = default;
(1) (since C++26)
constexpr indeterminate_domain( auto&& ) noexcept {}
(2) (since C++26)
1) Default constructor.
2) Converting constructor: any object can be converted to an indeterminate_domain, and the argument is ignored. This makes every domain convertible to an indeterminate_domain, as is needed for an indeterminate_domain to be the common type of domains.

execution::indeterminate_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)

Returns execution::default_domain().transform_sender(Tag(), std::forward<Sndr>(sndr), env): the sender that the standard library would use, see default_domain.

The program is ill-formed if, for some type D in Domains..., D().transform_sender(Tag(), std::forward<Sndr>(sndr), env) is well-formed and its type, ignoring references and cv-qualifiers, is not that of the sender returned by default_domain. That is, every domain that the work might complete in must either not transform the sender or transform it the same way as default_domain.

Helper classes

std::common_type<execution::indeterminate_domain>

std::common_type is specified for indeterminate_domain as follows:

  • std::common_type_t<indeterminate_domain<As...>, indeterminate_domain<Bs...>> is indeterminate_domain<Us...>, where Us... are the types As..., Bs... with duplicates removed;
  • for a type D that is not a specialization of indeterminate_domain, std::common_type_t<indeterminate_domain<As...>, D> is D if As... is empty, and std::common_type_t<indeterminate_domain<As...>, indeterminate_domain<D>> otherwise.

That is, the common type collects all the domains in which the work might complete, and indeterminate_domain<>, which names no domain, does not change the result.

Notes

  • Programs rarely need to name indeterminate_domain themselves. It is the answer of get_completion_domain for senders such as those of when_all whose children complete in different domains. It can also be the domain of the error completion of an adaptor such as then, which fails both where its child sender fails and where the function throws, when those are in different domains.
  • Two domains have a common type, and the work completes in that domain instead of in an indeterminate_domain, if one of them is convertible to the other, for example because it is derived from it, or if std::common_type is specialized for them. Whether two domains derived from default_domain should have default_domain as their common domain is the subject of LWG issue 4570.
  • An indeterminate_domain has no member function apply_sender, so apply_sender, and with it this_thread::sync_wait, uses the implementation of default_domain for work that completes in an indeterminate_domain.
  • P3826R5 added indeterminate_domain before C++26 was published, together with get_completion_domain.

Example

The when_all of two senders that complete in the same domain completes in that domain, and the when_all of two senders that complete in different domains completes in an indeterminate_domain. then can still be used with the latter, because neither domain replaces 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 <execution>
#include <print>
#include <utility>

namespace ex = std::execution;

// The domains of two execution resources. Neither replaces then
struct gpu_domain {};
struct fpga_domain {};

// A sender that reports Domain as the domain in which it completes
template<class Domain, ex::sender Sndr>
struct completes_in
{
    using sender_concept = ex::sender_tag;
    Sndr sndr;

    struct attrs
    {
        Domain query(ex::get_completion_domain_t<ex::set_value_t>) const
            noexcept
        {
            return {};
        }
    };

    template<class Self, class... Env>
    static consteval auto get_completion_signatures()
    {
        return ex::get_completion_signatures<Sndr, Env...>();
    }
    template<class Rcvr>
    auto connect(Rcvr rcvr) &&
    {
        return ex::connect(std::move(sndr), std::move(rcvr));
    }
    attrs get_env() const noexcept { return {}; }
};

template<class Domain>
auto in(ex::sender auto sndr)
{
    return completes_in<Domain, decltype(sndr)>{std::move(sndr)};
}

template<class D>
constexpr bool is_indeterminate = false;
template<class... Ds>
constexpr bool is_indeterminate<ex::indeterminate_domain<Ds...>> = true;

template<class Sndr>
constexpr bool completes_in_indeterminate_domain(const Sndr& sndr)
{
    using D = decltype(ex::get_completion_domain<ex::set_value_t>(
        ex::get_env(sndr), ex::env<>()));
    return is_indeterminate<D>;
}

int main()
{
    // when_all completes where the child that finishes last completes
    auto same = ex::when_all(in<gpu_domain>(ex::just(1)),
                             in<gpu_domain>(ex::just(2)));
    auto mixed = ex::when_all(in<gpu_domain>(ex::just(1)),
                              in<fpga_domain>(ex::just(2)));
    std::println("Indeterminate with children in one domain: {}",
                 completes_in_indeterminate_domain(same));
    std::println("Indeterminate with children in two domains: {}",
                 completes_in_indeterminate_domain(mixed));

    // Neither domain replaces then, so the default implementation is used.
    // If one of them replaced it, this would not compile
    auto sum = std::move(mixed) | ex::then([](int a, int b) { return a + b; });
    auto [n] = std::this_thread::sync_wait(std::move(sum)).value();
    std::println("Sum: {}", n);
}

Output:

Indeterminate with children in one domain: false
Indeterminate with children in two domains: true
Sum: 3

See also

execution domain of the standard library's own implementations of the sender algorithms
(class) [edit]
asks the completion domain associated with a completion tag from a sender's attributes
(customization point object)[edit]
asks a queryable object for its associated execution domain tag
(customization point object)[edit]
transforms into a new sender under a given execution domain tag
(function template) [edit]
determines the common type of a group of types
(class template) [edit]