命名空间
变体
操作

std::experimental::pmr::polymorphic_allocator<T>::construct

来自 cppreference.cn
 
 
 
 
 
template< class U, class... Args >
void construct( U* p, Args&&... args );
(1) (库基础 TS)
template< class T1, class T2, class... Args1, class... Args2 >

void construct( std::pair<T1, T2>* p,
                std::piecewise_construct_t,
                std::tuple<Args1...> x,

                std::tuple<Args2...> y );
(2) (库基础 TS)
template< class T1, class T2 >
void construct( std::pair<T1, T2>* p );
(3) (库基础 TS)
template< class T1, class T2, class U, class V >
void construct( std::pair<T1, T2>* p, U&& x, V&& y );
(4) (库基础 TS)
template< class T1, class T2, class U, class V >
void construct( std::pair<T1, T2>* p, const std::pair<U, V>& xy );
(5) (库基础 TS)
template< class T1, class T2, class U, class V >
void construct( std::pair<T1, T2>* p, std::pair<U, V>&& xy );
(6) (库基础 TS)

在已分配但未初始化的存储中构造对象,该存储由提供的构造函数参数 p 指向。如果对象类型本身使用分配器,或者如果是 std::pair,则将 this->resource() 向下传递给构造的对象。

1) 如果 std::uses_allocator<U, memory_resource*>::value == false (类型 U 不使用分配器)且 std::is_constructible<U, Args...>::value == true,则通过 ::new((void *) p) U(std::forward<Args>(args)...); 构造对象。

否则,如果 std::uses_allocator<U, memory_resource*>::value == true (类型 U 使用分配器,例如,它是一个容器)且 std::is_constructible<U, std::allocator_arg_t, memory_resource*, Args...>::value == true,则通过 ::new((void *) p) U(std::allocator_arg, this->resource(), std::forward<Args>(args)...); 构造对象。

否则,如果 std::uses_allocator<U, memory_resource*>::value == true (类型 U 使用分配器,例如,它是一个容器)且 std::is_constructible<U, Args..., memory_resource*>::value == true,则通过 ::new((void *) p) U(std::forward<Args>(args)..., this->resource()); 构造对象。

否则,程序是非良构的。

2) 首先,如果 T1T2 是 allocator-aware 的,则根据以下三个规则修改元组 xy 以包含 this->resource(),从而得到两个新的元组 xprimeyprime

2a) 如果 T1 不是 allocator-aware 的 (std::uses_allocator<T1, memory_resource*>::value == false) 且 std::is_constructible<T1, Args1...>::value == true,则 xprime 是未修改的 x

2b) 如果 T1 是 allocator-aware 的 (std::uses_allocator<T1, memory_resource*>::value == true),并且其构造函数接受分配器标签 (std::is_constructible<T1, std::allocator_arg_t, memory_resource*, Args1...>::value == true,则 xprimestd::tuple_cat(std::make_tuple(std::allocator_arg, this->resource()), std::move(x))

2c) 如果 T1 是 allocator-aware 的 (std::uses_allocator<T1, memory_resource*>::value == true),并且其构造函数将分配器作为最后一个参数 (std::is_constructible<T1, Args1..., memory_resource*>::value == true),则 xprimestd::tuple_cat(std::move(x), std::make_tuple(this->resource()))

2d) 否则,程序是非良构的。

相同的规则适用于 T2yprime 替换 y

一旦构造了 xprimeyprime,就如同通过 ::new((void *) p) pair<T1, T2>(std::piecewise_construct, std::move(xprime), std::move(yprime)); 一样在已分配的存储中构造 pair p

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 - 其两个成员是 T1T2 的构造函数参数的 pair

[编辑] 返回值

(无)

[编辑] 注解

此函数由任何 allocator-aware 对象(例如,std::vector)调用(通过 std::allocator_traits),该对象被赋予 std::polymorphic_allocator 作为要使用的分配器。由于 memory_resource* 隐式转换为 polymorphic_allocator,因此内存资源指针将传播到任何使用多态分配器的 allocator-aware 子对象。

[编辑] 参见

[静态]
在已分配的存储中构造对象
(函数模板) [编辑]
(C++20 之前)
在已分配的存储中构造对象
(std::allocator<T> 的公共成员函数) [编辑]