Namespaces
Variants

std::forwarding_query

From cppreference.com
 
 
Execution control library
Concepts
 
Defined in header <execution>
struct forwarding_query_t { /* unspecified */ };
(1) (since C++26)
inline constexpr forwarding_query_t forwarding_query{};
(2) (since C++26)
(customization point object)
Call signature
template< class Query >
constexpr bool forwarding_query( Query q ) noexcept;
(3) (since C++26)

Asks a query object whether sender adaptors forward it, that is, whether senders nested inside other senders can use the query to ask the environment of the receiver of the outer operation.

When a sender adaptor such as execution::then or execution::let_value is connected to a receiver, it connects its child senders to receivers of its own. The environments of those receivers answer only the forwarding queries of the environment of the outer receiver, so a query that is not forwarding reaches only the sender that is connected directly to the receiver that answers it, such as the child of execution::write_env. In the same way, the attributes of a sender adaptor with one child (see execution::get_env) answer only the forwarding queries of the attributes of the child, except for the completion scheduler and completion domain queries, which the adaptor answers itself. A coroutine execution::task also passes on only the forwarding queries of its environment to the senders it awaits.

All standard queries are forwarding, except execution::get_forward_progress_guarantee, which is asked of schedulers. A query type that a program defines is not forwarding unless it opts in, either with a member function static constexpr bool query(std::forwarding_query_t) noexcept that returns true, or by deriving from std::forwarding_query_t.

3) The expression forwarding_query(q), where q is of type Q, is
  • q.query(forwarding_query) if that expression is well-formed. The program is ill-formed if that expression can throw an exception, if its type is not bool, or if it is not a constant expression when q is a constant expression;
  • otherwise, true if std::derived_from<Q, forwarding_query_t> is true;
  • otherwise, false.

Parameters

q - the query object to ask

Return value

true if q is a forwarding query, false otherwise.

Customization point objects

The name forwarding_query denotes a customization point object, which is a const function object of a literal semiregular class type. The cv-unqualified version of its type is a tag type denoted as forwarding_query_t. See CustomizationPointObject for details.

Notes

  • A query should be forwarding if its answer applies to the whole operation and to all work nested inside it, for example the allocator, the stop token, or the scheduler on which the operation was started. It should not be forwarding if its answer applies only to the one receiver or sender that provides it, which an adaptor between them would misrepresent.
  • forwarding_query(q) is a constant expression for the standard queries, and for every query object that is a constant expression, so it can be used in static_assert, if constexpr and in constraints.
  • Unlike most of the execution library, forwarding_query and forwarding_query_t are in namespace std, not std::execution.
  • Before P2300R6, the proposal had four separate queries, execution::forwarding_env_query, execution::forwarding_scheduler_query, execution::forwarding_receiver_query and execution::forwarding_sender_query, and queries opted in with tag_invoke instead of a member function query. Older implementations and articles may show the earlier interface.

Example

A program defines two queries, one forwarding and one not. Both are provided with write_env, but only the forwarding query reaches a sender that is nested inside let_value or unstoppable.
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;

// A query that sender adaptors forward to the senders they wrap
struct get_request_id_t
{
    static constexpr bool query(std::forwarding_query_t) noexcept
    {
        return true;
    }
    int operator()(const auto& env) const noexcept { return env.query(*this); }
};
inline constexpr get_request_id_t get_request_id{};

// A query that is not forwarded
struct get_label_t
{
    auto operator()(const auto& env) const noexcept { return env.query(*this); }
};
inline constexpr get_label_t get_label{};

static_assert(std::forwarding_query(get_request_id));
static_assert(!std::forwarding_query(get_label));
static_assert(std::forwarding_query(std::get_stop_token));
static_assert(!std::forwarding_query(ex::get_forward_progress_guarantee));

// A sender that prints what the environment of its receiver provides
template<class Rcvr>
struct probe_op
{
    using operation_state_concept = ex::operation_state_t;
    Rcvr rcvr;

    void start() & noexcept
    {
        auto env = ex::get_env(rcvr);
        if constexpr (requires { env.query(get_request_id); })
            std::print("request {}", env.query(get_request_id));
        else
            std::print("no request id");
        if constexpr (requires { env.query(get_label); })
            std::println(", label {}", env.query(get_label));
        else
            std::println(", no label");
        ex::set_value(std::move(rcvr));
    }
};

struct probe
{
    using sender_concept = ex::sender_t;
    using completion_signatures = ex::completion_signatures<ex::set_value_t()>;

    template<class Rcvr>
    probe_op<Rcvr> connect(Rcvr rcvr) const { return {std::move(rcvr)}; }
};

int main()
{
    auto env = ex::env{ex::prop(get_request_id, 42), ex::prop(get_label, "A")};

    std::print("Connected directly: ");
    std::this_thread::sync_wait(ex::write_env(probe{}, env));

    std::print("Inside let_value:   ");
    auto nested = ex::just() | ex::let_value([] { return probe{}; });
    std::this_thread::sync_wait(ex::write_env(nested, env));

    std::print("Inside unstoppable: ");
    std::this_thread::sync_wait(ex::write_env(ex::unstoppable(probe{}), env));
}

Output:

Connected directly: request 42, label A
Inside let_value:   request 42, no label
Inside unstoppable: request 42, no label

See also

returns the associated queryable object for its given argument
(customization point object)[edit]
creates a sender that queries its receiver's associated environment
(customization point object)[edit]
adapts a sender so that the operations inside it see extra or different values in their execution environment
(customization point object)[edit]
builds a queryable object from a query object and a value
(class template) [edit]
aggregates several queryable objects into one queryable object
(class template) [edit]