命名空间
变体
操作

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

来自 cppreference.cn
 
 
内存管理库
(仅作说明*)
未初始化内存算法
(C++17)
(C++17)
(C++17)
受约束的未初始化
内存算法
C 库

分配器
内存资源
垃圾回收支持
(C++11)(直到 C++23)
(C++11)(直到 C++23)
(C++11)(直到 C++23)
(C++11)(直到 C++23)
(C++11)(直到 C++23)
(C++11)(直到 C++23)
未初始化存储
(直到 C++20*)
(直到 C++20*)
显式生命周期管理
 
 
template< class U, class... Args >
void construct( U* p, Args&&... args );
(1) (C++17 起)
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) (C++17 起)
(C++20 前)
template< class T1, class T2 >
void construct( std::pair<T1, T2>* p );
(3) (C++17 起)
(C++20 前)
template< class T1, class T2, class U, class V >
void construct( std::pair<T1, T2>* p, U&& x, V&& y );
(4) (C++17 起)
(C++20 前)
template< class T1, class T2, class U, class V >
void construct( std::pair<T1, T2>* p, const std::pair<U, V>& xy );
(5) (C++17 起)
(C++20 前)
template< class T1, class T2, class U, class V >
void construct( std::pair<T1, T2>* p, std::pair<U, V>&& xy );
(6) (C++17 起)
(C++20 前)
template< class T1, class T2, class NonPair >
void construct( std::pair<T1, T2>* p, NonPair&& non_pair );
(7) (C++17 起)
(C++20 前)

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

1) 使用 uses-allocator construction 在 `p` 指示的未初始化内存位置创建给定类型 `U` 的对象,并使用 `*this` 作为分配器。此重载仅在 `U` 不是 std::pair 的特化时才参与重载决议。(直到 C++20)
2) 首先,如果 `T1` 或 `T2` 是分配器感知的,则根据以下三个规则修改元组 `x` 和 `y` 以包含 `this->resource()`,从而产生两个新元组 `xprime` 和 `yprime`。
2a) 如果 `T1` 不是分配器感知的 (std::uses_allocator<T1, polymorphic_allocator>::value==false) 且 std::is_constructible<T1, Args1...>::value==true,则 `xprime` 是未修改的 `x`。
2b) 如果 `T1` 是分配器感知的 (std::uses_allocator<T1, polymorphic_allocator>::value==true),并且其构造函数接受分配器标签 (std::is_constructible<T1, std::allocator_arg_t, polymorphic_allocator, Args1...>::value==true),则 `xprime` 是 std::tuple_cat(std::make_tuple(std::allocator_arg, *this), std::move(x))
2c) 如果 `T1` 是分配器感知的 (std::uses_allocator<T1, polymorphic_allocator>::value==true),并且其构造函数将分配器作为最后一个参数 (std::is_constructible<T1, Args1..., polymorphic_allocator>::value==true),则 `xprime` 是 std::tuple_cat(std::move(x), std::make_tuple(*this))
2d) 否则,程序是格式错误的。
同样的规则适用于 `T2` 和将 `y` 替换为 `yprime`。
一旦构造了 `xprime` 和 `yprime`,就在已分配的存储中构造 `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<>()),也就是说,如果对的成员类型接受内存资源,则将其传递给它们。
5) 等同于
6) 等价于
7) 只有在给定仅用于阐述的函数模板时,此重载才参与重载决议
template< class A, class B >
void /*deduce-as-pair*/( const std::pair<A, B>& );

, /*deduce-as-pair*/(non_pair) 在作为未求值操作数考虑时是格式错误的。等价于

construct<T1, T2, T1, T2>(p, std::forward<NonPair>(non_pair));
(C++20 前)

目录

[编辑] 参数

p - 指向已分配但未初始化的存储的指针
args... - 要传递给 T 构造函数的构造函数参数
x - 要传递给 T1 构造函数的构造函数参数
y - 要传递给 T2 构造函数的构造函数参数
xy - 其两个成员为 T1T2 构造函数参数的 pair
non_pair - 非 `pair` 参数,用于转换为 `pair` 以进行进一步构造

[编辑] 返回值

(无)

[编辑] 注意

此函数由任何分配器感知对象(例如 std::pmr::vector 或另一个使用 `std::pmr::polymorphic_allocator` 作为分配器的 std::vector)通过 std::allocator_traits 调用。

[编辑] 缺陷报告

下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。

缺陷报告 应用于 发布时的行为 正确的行为
LWG 2969 C++17 uses-allocator construction 传递了 `resource()` 传递 `*this`
LWG 2975 C++17 第一个重载在某些情况下被错误地用于对构造 限制为不接受对
LWG 3525 C++17 没有重载可以处理可转换为对的非 `pair` 类型 添加了重构重载

[编辑] 参阅

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