std::scoped...::construct(3)
πΈ NAME πΈ
okay so bestie, this is the manual page for std::scoped_allocator_adaptor::construct β filed under std::scoped...::construct(3) β from the C++ Standard Library. The header spells it βC++ Standard Libaryβ and honestly? iconic. The NAME section is basically the same name twice with a dash, so: std::scoped_allocator_adaptor::construct β thatβs the construct member of std::scoped_allocator_adaptor. periodt. π
β¨ Synopsis β¨
Defined in header <scoped_allocator>
template< class T, class... Args >
(1)
void construct( T* p, Args&&... args );
template< class T1, class T2, class... Args1, class... Args2 >
(2) (until C++20)
void construct( std::pair<T1, T2>* p,
std::piecewise_construct_t,
std::tuple<Args1...> x,
std::tuple<Args2...> y );
template< class T1, class T2 >
(3) (until C++20)
void construct( std::pair<T1, T2>* p );
template< class T1, class T2, class U, class V >
(4) (until C++20)
void construct( std::pair<T1, T2>* p, U&& x, V&& y );
template< class T1, class T2, class U, class V >
(5) (until C++20)
void construct( std::pair<T1, T2>* p, const std::pair<U, V>& xy );
template< class T1, class T2, class U, class V >
(6) (until C++20)
void construct( std::pair<T1, T2>* p, std::pair<U, V>&& xy );
template< class T1, class T2, class NonPair >
(7) (until C++20)
void construct( std::pair<T1, T2>* p, NonPair&& non_pair );
π Description π
The big picture: this thing constructs an object in allocated, but not initialized storage pointed to by p, using OuterAllocator and the constructor arguments you pass in. If the object is of a type that itself uses allocators, or if it is a std::pair, it passes InnerAllocator down to the constructed object. allocator nepotism, but make it standards-compliant. π«Ά
First, it retrieves the outermost allocator OUTERMOST by calling this->outer_allocator(), and then calling the outer_allocator() member function recursively on the result of this call until reaching an allocator that has no such member function.
Then it defines OUTERMOST_ALLOC_TRAITS(x) as std::allocator_traits<std::remove_reference_t<decltype(OUTERMOST(x))>>.
1) Creates an object of the given type T by means of uses-allocator construction at the uninitialized memory location indicated by p, using OUTERMOST as the allocator. After adjustment for the uses-allocator convention expected by Tβs constructor, it calls OUTERMOST_ALLOC_TRAITS(*this)::construct.
Until C++20, this overload participates in overload resolution only if U is not a specialization of std::pair.
Equivalent to (since C++20):
std::apply(
[p, this](auto&&... newargs)
{
OUTERMOST_ALLOC_TRAITS(*this)::construct(
OUTERMOST(*this), p,
std::forward<decltype(newargs)>(newargs)...);
},
std::uses_allocator_construction_args(
inner_allocator(),
std::forward<Args>(args)...
)
);
2) First, if either T1 or T2 is allocator-aware, it modifies the tuples x and y to include the appropriate inner allocator, resulting in the two new tuples xprime and yprime, according to these three rules:
- 2a) if
T1is not allocator-aware (std::uses_allocator<T1, inner_allocator_type>::value == false), thenxprimeisstd::tuple<Args1&&...>(std::move(x)). (It is also required thatstd::is_constructible<T1, Args1...>::value == true). - 2b) if
T1is allocator-aware (std::uses_allocator<T1, inner_allocator_type>::value == true), and its constructor takes an allocator tag wherestd::is_constructible<T1, std::allocator_arg_t, inner_allocator_type&, Args1...>::value == true, thenxprimeis:
std::tuple_cat(std::tuple<std::allocator_arg_t,
inner_allocator_type&>(
std::allocator_arg, inner_allocator()
),
std::tuple<Args1&&...>(std::move(x)))
- 2c) if
T1is allocator-aware (std::uses_allocator<T1, inner_allocator_type>::value == true), and its constructor takes the allocator as the last argument wherestd::is_constructible<T1, Args1..., inner_allocator_type&>::value == true, thenxprimeis:
std::tuple_cat(std::tuple<Args1&&...>(std::move(x)),
std::tuple<inner_allocator_type&>(inner_allocator()))
Same rules apply to T2 and the replacement of y with yprime.
Once xprime and yprime are constructed, it constructs the pair p in allocated storage by calling (until C++20):
std::allocator_traits<O>::construct(OUTERMOST,
p,
std::piecewise_construct,
std::move(xprime),
std::move(yprime));
3) Equivalent to:
construct(p, std::piecewise_construct, std::tuple<>(),
std::tuple<>()),
that is, it passes the inner allocator on to the pairβs member types if they accept them.
4) Equivalent to:
construct(p, std::piecewise_construct,
std::forward_as_tuple(std::forward<U>(x)),
std::forward_as_tuple(std::forward<V>(y)))
5) Equivalent to:
construct(p, std::piecewise_construct,
std::forward_as_tuple(xy.first),
std::forward_as_tuple(xy.second))
6) Equivalent to:
construct(p, std::piecewise_construct,
std::forward_as_tuple(std::forward<U>(xy.first)),
std::forward_as_tuple(std::forward<V>(xy.second)))
7) This overload participates in overload resolution only if given the exposition-only function template:
template<class A, class B>
void /*deduce-as-pair*/(const std::pair<A, B>&);,
/*deduce-as-pair*/(non_pair) is ill-formed when considered as an unevaluated operand.
Equivalent to construct<T1, T2, T1, T2>(p, std::forward<NonPair>(non_pair));.
π Parameters π
pβ pointer to allocated, but not initialized storageargs...β the constructor arguments to pass to the constructor ofTxβ the constructor arguments to pass to the constructor ofT1yβ the constructor arguments to pass to the constructor ofT2xyβ the pair whose two members are the constructor arguments forT1andT2non_pairβ non-pair argument to convert to pair for further construction
π Return value π
(none)
π¦ Notes π¦
This function is called (through std::allocator_traits) by any allocator-aware object, such as std::vector, that was given a std::scoped_allocator_adaptor as the allocator to use. Since inner_allocator is itself an instance of std::scoped_allocator_adaptor, this function will also be called when the allocator-aware objects constructed through this function start constructing their own members. itβs recursive bestie behavior, but make it allocator lore. π
π Defect reports π
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
- LWG 2975 Β· applied to C++11 β as published: first overload is mistakenly used for pair construction in some cases; correct behavior: constrained to not accept pairs.
- P0475R1 Β· applied to C++11 β as published: pair piecewise construction may copy the arguments; correct behavior: transformed to tuples of references to avoid copy.
- LWG 3525 Β· applied to C++11 β as published: no overload could handle non-pair types convertible to pair; correct behavior: reconstructing overload added.
π₯ See also π₯
construct[static]β constructs an object in the allocated storage (function template)construct(until C++20)β constructs an object in allocated storage (public member function ofstd::allocator<T>)
okay bestie, thatβs the whole page π source/version/date check: http://cppreference.com, 2024.06.10, page std::scoped...::construct(3). periodt. π«Ά
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms