Namespaces
Variants

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

From cppreference.net
Memory management library
( exposition only* )
Allocators
Uninitialized memory algorithms
Constrained uninitialized memory algorithms
Memory resources
Uninitialized storage (until C++20)
( until C++20* )
( until C++20* )
( until C++20* )

Garbage collector support (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
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) 在由 p 指定的未初始化内存位置,使用 * this 作为分配器,通过 使用分配器构造 方式创建给定类型 U 的对象。 此重载仅当 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 ,就在已分配存储中构造 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 的成员类型接受内存资源,则将内存资源传递给它们。
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 - 其两个成员分别为 T1 T2 构造参数的对
non_pair - 需转换为 pair 以进行后续构造的非 pair 参数

返回值

(无)

注释

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

缺陷报告

下列行为变更缺陷报告被追溯应用于先前发布的 C++ 标准。

缺陷报告 适用版本 发布时行为 正确行为
LWG 2969 C++17 使用分配器构造传递了 resource() 传递 * this
LWG 2975 C++17 某些情况下错误使用首个重载进行 pair 构造 约束为不接受 pair 类型
LWG 3525 C++17 没有重载能处理可转换为 pair 的非 pair 类型 添加了重构重载

参见

[static]
在已分配的存储空间中构造对象
(函数模板)
(until C++20)
在已分配的存储空间中构造对象
( std::allocator<T> 的公开成员函数)