std::execution::env
| Defined in header <execution>
|
||
template< /* queryable */... Envs >
struct env;
|
(since C++26) | |
The class template env combines zero or more queryable objects into a single queryable object. A query is forwarded to the first stored object, in the order of Envs, whose query member accepts it.
Specializations of env are not assignable. It is unspecified whether initialization with a parenthesized expression list is supported, except when the list contains a single expression of type env or const env. Use braces to initialize an environment from its constituent objects.
Template parameters
| Envs | - | types of the constituent queryable objects |
Member functions
query |
forwards a query to the first constituent object that accepts it (public member function) |
template< class QueryTag, class... Args >
constexpr decltype(auto) query( QueryTag q, Args&&... args ) const noexcept(/* see below */);
|
(since C++26) | |
This function participates in overload resolution only if at least one Env in Envs satisfies the following exposition-only concept:
template<class Env, class QueryTag, class... Args>
concept has_query = requires(const Env& e, Args&&... args)
{
e.query(QueryTag(), std::forward<Args>(args)...);
};
Let fe be the first constituent object for which fe.query(q, std::forward<Args>(args)...) is well-formed. Equivalent to return fe.query(q, std::forward<Args>(args)...);. The exception specification is equivalent to noexcept(fe.query(q, std::forward<Args>(args)...)).
Deduction guides
template< class... Envs >
env( Envs... ) -> env<std::unwrap_reference_t<Envs>...>;
|
(since C++26) | |
This deduction guide unwraps std::reference_wrapper so that constituent objects can be held by reference.
Example
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 <memory_resource>
#include <print>
#include <stop_token>
namespace ex = std::execution;
struct stop_environment
{
std::stop_token token;
std::stop_token query(std::get_stop_token_t) const noexcept
{
return token;
}
};
struct allocator_environment
{
std::pmr::polymorphic_allocator<> query(std::get_allocator_t) const noexcept
{
return resource;
}
std::pmr::memory_resource* resource;
};
int main()
{
std::stop_source first;
std::stop_source second;
std::pmr::monotonic_buffer_resource mbr;
// Both stop environments answer get_stop_token.
// The first matching constituent takes precedence.
auto env = ex::env{
stop_environment{first.get_token()},
stop_environment{second.get_token()},
allocator_environment{&mbr}
};
auto token = std::get_stop_token(env);
auto alloc = std::get_allocator(env);
second.request_stop();
std::println("stop requested: {}", token.stop_requested());
first.request_stop();
std::println("stop requested: {}", token.stop_requested());
std::println("allocator use `mbr` as memory_resource: {}", alloc == &mbr);
}
Output:
stop requested: false
stop requested: true
allocator use `mbr` as memory_resource: true
See also
(C++26)(C++26) |
returns the associated queryable object for its given argument (customization point object) |
(C++26) |
creates a sender that queries its receiver's associated environment (customization point object) |
(C++26) |
adapts a sender so that the operations inside it see extra or different values in their execution environment (customization point object) |