命名空间
变体
操作

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 是分配器感知类型,则根据以下三条规则修改元组 xy 以包含 this->resource(),从而得到两个新的元组 xprimeyprime

2a) 如果 T1 不是分配器感知类型 (std::uses_allocator<T1, memory_resource*>::value == false) 且 std::is_constructible<T1, Args1...>::value == true,则 xprime 为未修改的 x

2b) 如果 T1 是分配器感知类型 (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 是分配器感知类型 (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) 否则,程序格式错误。

同样的规则适用于 T2yyprime 替换的情况。

一旦 xprimeyprime 被构造,就在已分配的存储中构造 pair p,如同通过 ::new((void *) p) pair<T1, T2>(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 - 其两个成员为 T1T2 构造函数参数的 pair

[编辑] 返回值

(无)

[编辑] 注意

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

[编辑] 参阅

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