定义于头文件
<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) | ||
用 OuterAllocator 和给定的构造函数参数,在 p
所指向的被分配而未初始化的存储上构造对象。若对象的类型自身使用分配器或是 std::pair ,则向下传递 InnerAllocator 给被构造的对象。
首先,确定最外层分配器类型 OUTERMOST
:它是调用 this->outer_allocator() ,然后在此调用结果上递归调用成员函数 outer_allocator()
,直至抵达无该成员函数的类型的返回类型。该类型为最外层分配器。
然后:
1) 若 std::uses_allocator<T, inner_allocator_type>::value==false (类型 T
不使用分配器)且若 std::is_constructible<T, Args...>::value==true ,则调用
std::allocator_traits<OUTERMOST>::construct( OUTERMOST(*this),
p,
std::forward<Args>(args)... );
否则,若 std::uses_allocator<T, inner_allocator_type>::value==true (类型 T
使用分配器,例如它是容器)且若 std::is_constructible<T, std::allocator_arg_t, inner_allocator_type&, Args...>::value==true ,则调用
std::allocator_traits<OUTERMOST>::construct( OUTERMOST(*this),
p,
std::allocator_arg,
inner_allocator(),
std::forward<Args>(args)... );
否则,若 std::uses_allocator<T, inner_allocator_type>::value==true (类型 T
使用分配器,例如它是容器)且若 std::is_constructible<T, Args..., inner_allocator_type&>::value==true ,则调用
std::allocator_traits<OUTERMOST>::construct( OUTERMOST(*this),
p,
std::forward<Args>(args)...,
inner_allocator());
否则,发布编译错误,因为尽管 std::uses_allocator<T>
表示 T
具分配器,它却缺少接受分配器的构造函数形式。
2) 首先,若 T1
或 T2
具分配器,则修改 tuple x
和 y
以包含适当的内层分配器,产生二个新的 tuple xprime
与 yprime
,根据下列三条规则:
2a) 若 T1
不具分配器( std::uses_allocator<T1, inner_allocator_type>::value==false ),则 xprime
是不修改的 x
。(亦要求 std::is_constructible<T1, Args1...>::value==true )。
2b) 若 T1
具分配器( std::uses_allocator<T1, inner_allocator_type>::value==true ),且其构造函数接收分配器标签( std::is_constructible<T1, std::allocator_arg_t, inner_allocator_type&, Args1...>::value==true ),则 xprime
是
std::tuple_cat( std::tuple<std::allocator_arg_t, inner_allocator_type&>( std::allocator_arg,
inner_allocator()
), std::move(x))
2c) 若 T1
具分配器( std::uses_allocator<T1, inner_allocator_type>::value==true ),且其构造函数接收分配器为最后参数( std::is_constructible<T1, Args1..., inner_allocator_type&>::value==true ),则 xprime
是 std::tuple_cat(std::move(x), std::tuple<inner_allocator_type&>(inner_allocator())) 。
同样的规则应用于 T2
和以 yprime
替换 y
一旦构造了 xprime
和 yprime
(这亦要求所有 Args1... 与 Args2... 中的类型为可复制构造 (CopyConstructible
) ),则在分配的存储构造 pair p
,通过调用
std::allocator_traits<OUTERMOST>::construct( OUTERMOST(*this),
p,
std::piecewise_construct,
std::move(xprime),
std::move(yprime));
3) 等价于 construct(p, std::piecewise_construct, std::tuple<>(), std::tuple<>()) ,即传递内层分配器到 pair 的成员类型上,若它们接受它。
4) 等价于
construct(p, std::piecewise_construct, std::forward_as_tuple(std::forward<U>(x)),
std::forward_as_tuple(std::forward<V>(y)))
5) 等价于
construct(p, std::piecewise_construct, std::forward_as_tuple(xy.first),
std::forward_as_tuple(xy.second))
6) 等价于
construct(p, std::piecewise_construct, std::forward_as_tuple(std::forward<U>(xy.first)),
std::forward_as_tuple(std::forward<V>(xy.second)))
目录 |
p | - | 指向被分配但未初始化的存储的指针 |
args... | - | 传递给 T 的构造函数的构造函数参数
|
x | - | 传递给 T1 的构造函数的构造函数参数
|
y | - | 传递给 T2 的构造函数的构造函数参数
|
xy | - | 成员分别为 T1 与 T2 构造函数实参的 pair
|
(无)
此函数为任何给予了 std::scoped_allocator_adaptor 作为分配器使用的具分配器对象,例如 std::vector 所调用(通过 std::allocator_traits )。因为 inner_allocator
自身是 std::scoped_allocator_adaptor 的实例,此函数亦在具分配器对象通过此函数开始构造其自身成员时得到调用。
[静态]
|
在分配的存储构造对象 (函数模板) |
(C++17 中弃用)
|
在分配的存储构造对象 ( std::allocator 的公开成员函数)
|