std::get_allocator
| Defined in header <execution>
|
||
struct get_allocator_t { /* unspecified */ };
|
(1) | (since C++26) |
inline constexpr get_allocator_t get_allocator{};
|
(2) | (since C++26) (customization point object) |
| Call signature |
||
template< class Env >
/* simple-allocator */ get_allocator( const Env& env ) noexcept;
|
(3) | (since C++26) |
A query that asks an environment, usually the environment of a receiver, for the allocator that an asynchronous operation should use for the memory it allocates. An operation that allocates obtains the allocator with get_allocator(execution::get_env(rcvr)), or with execution::read_env(get_allocator) inside a sender.
The caller decides which allocator an operation uses, through the environment of the receiver it connects. A program provides one with execution::write_env(sndr, execution::prop(std::get_allocator, alloc)), see write_env and prop. The query is forwarded through the environments of the receivers of sender adaptors, so nested operations use the same allocator unless an adaptor on the way changes it. The standard library uses the allocator as follows:
- execution::spawn and execution::spawn_future allocate the state of the spawned operation with the allocator of the environment passed to them, or, if it has none, with the allocator of the attributes of the sender, which they then also provide to the sender. If neither has one, they use
std::allocator<void>(). - When an operation state stores an argument of a sender factory or adaptor, such as a value passed to execution::just or a function passed to execution::then, and the environment of the receiver provides an allocator, the stored object is created by uses-allocator construction with that allocator.
- A coroutine execution::task allocates its coroutine frame with the allocator passed after std::allocator_arg at the start of the arguments of the coroutine, or with a default-constructed
allocator_typeotherwise. To the senders it awaits, it provides the allocator of its own receiver converted toallocator_type, or a default-constructedallocator_typeif the receiver has none.
get_allocator(env) returns std::as_const(env).query(get_allocator). If that expression is not well-formed, get_allocator(env) is not well-formed either. The program is ill-formed if the expression can throw an exception, or if its type does not meet the basic requirements of Allocator: an allocator whose allocate and deallocate can be called and that is copy-constructible and equality-comparable.Parameters
| env | - | the environment (or other queryable object) to query |
Return value
The allocator of env.
Customization point objects
The name get_allocator 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 get_allocator_t. See CustomizationPointObject for details.
Notes
- Unlike std::get_stop_token, the query has no default:
get_allocator(env)is ill-formed ifenvprovides no allocator, andexecution::read_env(get_allocator)cannot be connected to such a receiver. std::this_thread::sync_wait, for example, provides no allocator. An operation that is meant to work with any receiver checksrequires { get_allocator(env); }and uses std::allocator otherwise. - The value type of the allocator is not specified, so it may need to be rebound with
std::allocator_traits<Alloc>::rebind_alloc<T>to allocate objects of typeT.std::pmr::polymorphic_allocator<>is a convenient choice, because containers fromstd::pmraccept it directly. - Unlike most queries of the execution library,
get_allocatoris in namespacestd, notstd::execution. - Earlier revisions of the proposal also had a form of
get_allocatorwithout arguments, which returned a sender equivalent toexecution::read_env(get_allocator). It was removed before C++26 was published. Older implementations and articles may show the earlier form.
Example
An operation that keeps its data in memory from the allocator of its environment. spawn also allocates its own state with the allocator, and write_env adds the allocator to the environment that sync_wait provides.
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 <algorithm>
#include <cstddef>
#include <execution>
#include <functional>
#include <memory_resource>
#include <print>
#include <vector>
namespace ex = std::execution;
// A memory resource that reports what it allocates
struct reporting_resource : std::pmr::memory_resource
{
void* do_allocate(std::size_t n, std::size_t align) override
{
std::println(" allocate {} bytes", n);
return std::pmr::new_delete_resource()->allocate(n, align);
}
void do_deallocate(void* p, std::size_t n, std::size_t align) override
{
std::println(" deallocate {} bytes", n);
std::pmr::new_delete_resource()->deallocate(p, n, align);
}
bool do_is_equal(const memory_resource& other) const noexcept override
{
return this == &other;
}
};
// Work that keeps its data in memory from the allocator of its environment.
// It is noexcept because spawn accepts only work that cannot fail
auto work = ex::read_env(std::get_allocator)
| ex::then([](auto alloc) noexcept
{
std::pmr::vector<int> v({1, 2, 3, 4}, alloc);
int sum = std::ranges::fold_left(v, 0, std::plus{});
std::println(" sum: {}", sum);
});
int main()
{
reporting_resource resource;
auto env = ex::prop(std::get_allocator,
std::pmr::polymorphic_allocator<>(&resource));
// spawn allocates its own state with the allocator, and passes it on
std::println("spawn:");
ex::counting_scope scope;
ex::spawn(work, scope.get_token(), env);
std::this_thread::sync_wait(scope.join());
// sync_wait provides no allocator, but write_env can add one
std::println("sync_wait:");
std::this_thread::sync_wait(ex::write_env(work, env));
}
Possible output:
spawn:
allocate 80 bytes
allocate 16 bytes
sum: 10
deallocate 16 bytes
deallocate 80 bytes
sync_wait:
allocate 16 bytes
sum: 10
deallocate 16 bytes
See also
(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) |
(C++26) |
builds a queryable object from a query object and a value (class template) |
(C++26) |
eagerly launches the given sender within an asynchronous scope, returning immediately without waiting for it to finish (customization point object) |
(C++17) |
an allocator that supports run-time polymorphism based on the std::pmr::memory_resource it is constructed with (class template) |
(C++11) |
provides information about allocator types (class template) |