Difference between revisions of "cpp/memory/scoped allocator adaptor/construct"
(2975) |
(P0475) |
||
Line 34: | Line 34: | ||
Constructs an object in allocated, but not initialized storage pointed to by {{tt|p}} using OuterAllocator and the provided constructor arguments. If the object is of type that itself uses allocators, or if it is std::pair, passes InnerAllocator down to the constructed object. | Constructs an object in allocated, but not initialized storage pointed to by {{tt|p}} using OuterAllocator and the provided constructor arguments. If the object is of type that itself uses allocators, or if it is std::pair, passes InnerAllocator down to the constructed object. | ||
− | First, | + | First, retrieve the outermost allocator {{tt|OUTERMOST}} by calling {{c|this->outer_allocator()}}, and then calling the {{tt|outer_allocator()}} member function recursively on the result of this call until reaching an allocator that has no such member function. Let {{tt|O}} be the type of {{tt|OUTERMOST}}. |
Then: | Then: | ||
Line 41: | Line 41: | ||
{{c| | {{c| | ||
− | std::allocator_traits< | + | std::allocator_traits<O>::construct( OUTERMOST, |
− | + | p, | |
− | + | std::forward<Args>(args)... ); | |
}} | }} | ||
Line 49: | Line 49: | ||
{{c| | {{c| | ||
− | std::allocator_traits< | + | std::allocator_traits<O>::construct( OUTERMOST, |
− | + | p, | |
− | + | std::allocator_arg, | |
− | + | inner_allocator(), | |
− | + | std::forward<Args>(args)... ); | |
}} | }} | ||
Line 59: | Line 59: | ||
{{c| | {{c| | ||
− | std::allocator_traits< | + | std::allocator_traits<O>::construct( OUTERMOST, |
− | + | p, | |
− | + | std::forward<Args>(args)..., | |
− | + | inner_allocator()); | |
}} | }} | ||
− | Otherwise, | + | Otherwise, the program is ill-formed: even though {{tt|std::uses_allocator<T>}} claimed that {{tt|T}} is allocator-aware, it lacks either form of allocator-accepting constructors. |
{{cpp/enable if|{{tt|U}} is not a specialization of {{lc|std::pair}}}}. | {{cpp/enable if|{{tt|U}} is not a specialization of {{lc|std::pair}}}}. | ||
Line 71: | Line 71: | ||
2) First, if either {{tt|T1}} or {{tt|T2}} is allocator-aware, modifies the tuples {{tt|x}} and {{tt|y}} to include the appropriate inner allocator, resulting in the two new tuples {{tt|xprime}} and {{tt|yprime}}, according to the following three rules: | 2) First, if either {{tt|T1}} or {{tt|T2}} is allocator-aware, modifies the tuples {{tt|x}} and {{tt|y}} to include the appropriate inner allocator, resulting in the two new tuples {{tt|xprime}} and {{tt|yprime}}, according to the following three rules: | ||
− | 2a) if {{tt|T1}} is not allocator-aware ({{c|1=std::uses_allocator<T1, inner_allocator_type>::value==false}}, then {{tt|xprime}} is {{ | + | 2a) if {{tt|T1}} is not allocator-aware ({{c|1=std::uses_allocator<T1, inner_allocator_type>::value==false}}, then {{tt|xprime}} is {{c|std::tuple<Args1&&...>(std::move(x))}}. (it is also required that {{c|1=std::is_constructible<T1, Args1...>::value==true}}) |
− | 2b) if {{tt|T1}} is allocator-aware ({{c|1=std::uses_allocator<T1, inner_allocator_type>::value==true}}), and its constructor takes an allocator tag ({{c|1=std::is_constructible<T1, std::allocator_arg_t, inner_allocator_type&, Args1...>::value==true}}, then {{tt|xprime}} is | + | 2b) if {{tt|T1}} is allocator-aware ({{c|1=std::uses_allocator<T1, inner_allocator_type>::value==true}}), and its constructor takes an allocator tag ({{c|1=std::is_constructible<T1, std::allocator_arg_t, inner_allocator_type&, Args1...>::value==true}}), then {{tt|xprime}} is |
{{c|std::tuple_cat( std::tuple<std::allocator_arg_t, inner_allocator_type&>( std::allocator_arg, | {{c|std::tuple_cat( std::tuple<std::allocator_arg_t, inner_allocator_type&>( std::allocator_arg, | ||
inner_allocator() | inner_allocator() | ||
− | ), std::move(x))}} | + | ), std::tuple<Args1&&...>(std::move(x)))}} |
− | 2c) if {{tt|T1}} is allocator-aware ({{c|1=std::uses_allocator<T1, inner_allocator_type>::value==true}}), and its constructor takes the allocator as the last argument ({{c|1=std::is_constructible<T1, Args1..., inner_allocator_type&>::value==true}}), then {{tt|xprime}} is {{c|std::tuple_cat(std::move(x), std::tuple<inner_allocator_type&>(inner_allocator()))}}. | + | 2c) if {{tt|T1}} is allocator-aware ({{c|1=std::uses_allocator<T1, inner_allocator_type>::value==true}}), and its constructor takes the allocator as the last argument ({{c|1=std::is_constructible<T1, Args1..., inner_allocator_type&>::value==true}}), then {{tt|xprime}} is {{c|std::tuple_cat(std::tuple<Args1&&...>(std::move(x)), std::tuple<inner_allocator_type&>(inner_allocator()))}}. |
Same rules apply to {{tt|T2}} and the replacement of {{tt|y}} with {{tt|yprime}} | Same rules apply to {{tt|T2}} and the replacement of {{tt|y}} with {{tt|yprime}} | ||
− | Once {{tt|xprime}} and {{tt|yprime}} are constructed | + | Once {{tt|xprime}} and {{tt|yprime}} are constructed, constructs the pair {{tt|p}} in allocated storage by calling |
{{c| | {{c| | ||
− | std::allocator_traits< | + | std::allocator_traits<O>::construct( OUTERMOST, |
− | + | p, | |
− | + | std::piecewise_construct, | |
− | + | std::move(xprime), | |
− | + | std::move(yprime)); | |
}} | }} | ||
Line 134: | Line 134: | ||
{{dr list begin}} | {{dr list begin}} | ||
{{dr list item|wg=lwg|dr=2975|std=C++11|before=first overload is mistakenly used for pair construction in some cases|after=constrained to not accept pairs}} | {{dr list item|wg=lwg|dr=2975|std=C++11|before=first overload is mistakenly used for pair construction in some cases|after=constrained to not accept pairs}} | ||
+ | {{dr list item|paper=P0475R1|std=C++11|before=pair piecewise construction may copy the arguments|after=transformed to tuples of references to avoid copy}} | ||
{{dr list end}} | {{dr list end}} | ||
Revision as of 11:04, 10 June 2018
Defined in header <scoped_allocator>
|
||
template < class T, class... Args > void construct( T* p, Args&&... args ); |
(1) | |
template< class T1, class T2, class... Args1, class... Args2 > void construct( std::pair<T1, T2>* p, |
(2) | |
template< class T1, class T2 > void construct( std::pair<T1, T2>* p ); |
(3) | |
template< class T1, class T2, class U, class V > void construct( std::pair<T1, T2>* p, U&& x, V&& y ); |
(4) | |
(5) | ||
(6) | ||
Constructs an object in allocated, but not initialized storage pointed to by p
using OuterAllocator and the provided constructor arguments. If the object is of type that itself uses allocators, or if it is std::pair, passes InnerAllocator down to the constructed object.
First, retrieve 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. Let O
be the type of OUTERMOST
.
Then:
1) If std::uses_allocator<T, inner_allocator_type>::value==false (the type T
does not use allocators) and if std::is_constructible<T, Args...>::value==true, then calls
std::allocator_traits<O>::construct( OUTERMOST,
p,
std::forward<Args>(args)... );
Otherwise, if std::uses_allocator<T, inner_allocator_type>::value==true (the type T
uses allocators, e.g. it is a container) and if std::is_constructible<T, std::allocator_arg_t, inner_allocator_type&, Args...>::value==true, then calls
std::allocator_traits<O>::construct( OUTERMOST,
p,
std::allocator_arg,
inner_allocator(),
std::forward<Args>(args)... );
Otherwise, std::uses_allocator<T, inner_allocator_type>::value==true (the type T
uses allocators, e.g. it is a container) and if std::is_constructible<T, Args..., inner_allocator_type&>::value==true, then calls
std::allocator_traits<O>::construct( OUTERMOST,
p,
std::forward<Args>(args)...,
inner_allocator());
Otherwise, the program is ill-formed: even though std::uses_allocator<T>
claimed that T
is allocator-aware, it lacks either form of allocator-accepting constructors.
This overload participates in overload resolution only if U
is not a specialization of std::pair.
2) First, if either T1
or T2
is allocator-aware, modifies the tuples x
and y
to include the appropriate inner allocator, resulting in the two new tuples xprime
and yprime
, according to the following three rules:
2a) if T1
is not allocator-aware (std::uses_allocator<T1, inner_allocator_type>::value==false, then xprime
is std::tuple<Args1&&...>(std::move(x)). (it is also required that std::is_constructible<T1, Args1...>::value==true)
2b) if T1
is allocator-aware (std::uses_allocator<T1, inner_allocator_type>::value==true), and its constructor takes an allocator tag (std::is_constructible<T1, std::allocator_arg_t, inner_allocator_type&, Args1...>::value==true), then xprime
is
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 T1
is allocator-aware (std::uses_allocator<T1, inner_allocator_type>::value==true), and its constructor takes the allocator as the last argument (std::is_constructible<T1, Args1..., inner_allocator_type&>::value==true), then xprime
is 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, constructs the pair p
in allocated storage by calling
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, 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)))
Contents |
Parameters
p | - | pointer to allocated, but not initialized storage |
args... | - | the constructor arguments to pass to the constructor of T
|
x | - | the constructor arguments to pass to the constructor of T1
|
y | - | the constructor arguments to pass to the constructor of T2
|
xy | - | the pair whose two members are the constructor arguments for T1 and T2
|
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.
Defect reports
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR | Applied to | Behavior as published | Correct behavior |
---|---|---|---|
LWG 2975 | C++11 | first overload is mistakenly used for pair construction in some cases | constrained to not accept pairs |
P0475R1 | C++11 | pair piecewise construction may copy the arguments | transformed to tuples of references to avoid copy |
See also
[static] |
constructs an object in the allocated storage (function template) |
(until C++20) |
constructs an object in allocated storage (public member function of std::allocator<T> )
|