Namespaces
Variants

std::scoped_allocator_adaptor<OuterAlloc,InnerAlloc...>:: 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)
定义于头文件 <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, std:: piecewise_construct_t ,

std:: tuple < Args1... > x, std:: tuple < Args2... > y ) ;
(2) (C++20 前)
template < class T1, class T2 >
void construct ( std:: pair < T1, T2 > * p ) ;
(3) (C++20 前)
template < class T1, class T2, class U, class V >
void construct ( std:: pair < T1, T2 > * p, U && x, V && y ) ;
(4) (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++20 前)
template < class T1, class T2, class U, class V >
void construct ( std:: pair < T1, T2 > * p, std:: pair < U, V > && xy ) ;
(6) (C++20 前)
辅助函数模板
template < class T, class ... Args >
std:: tuple < /* see below */ > /*concat-args*/ ( std:: tuple < Args... > && tup ) ;
(7) ( 仅用于说明* )
(C++20 前)

在由 p 指向的已分配但未初始化的存储空间中,使用外部分配器及提供的构造函数参数构造对象。若该对象本身是使用分配器的类型 ,或为 std::pair (C++20 前) ,则向构造对象传递内部分配器。

1) 在由 p 指示的未初始化内存位置,通过 使用分配器构造 方式,利用最外层分配器构造类型 T 的对象。

给定 std:: uses_allocator < T, inner_allocator_type > :: value uses_inner

此重载仅当 T 不是 std::pair 的特化时才参与重载决议。

(C++20 前)

等价于 std:: apply
(
[ p, this ] ( auto && ... newargs )
{
outermost-construct
( p, std:: forward < decltype ( newargs ) > ( newargs ) ... ) ;
} ,
std:: uses_allocator_construction_args
( inner_allocator ( ) , std:: forward < Args > ( args ) ... )
) ;

(C++20 起)
2-6) 在由 p 指定的未初始化内存位置,通过 使用分配器构造 方式,使用最外层分配器构造一个 std::pair 对象。
2) xprime concat-args  < T1 > ( std :: move ( x ) ) yprime concat-args  < T2 > ( std :: move ( y ) ) ,调用 outermost-construct  ( p, std:: piecewise_construct , std :: move ( xprime ) , std :: move ( yprime ) )
3) 等价于 construct ( p, std:: piecewise_construct , std:: tuple <> ( ) , std:: tuple <> ( ) ) ;
4-6) 等价于 construct ( p, std:: piecewise_construct ,
std:: forward_as_tuple ( xarg ) , std:: forward_as_tuple ( yarg ) ) ;
,其中 xarg yarg 定义如下:
重载版本 xarg yarg
(4) std:: forward < U > ( x ) std:: forward < V > ( y )
(5) xy. first xy. second
(6) std:: forward < U > ( xy. first ) std:: forward < V > ( xy. second )
7) 合并 tup 中包含的参数以及 T 类型对象进行 使用分配器构造 所需的附加参数。
给定 std:: uses_allocator < T, inner_allocator_type > :: value uses_inner

目录

参数

p - 指向已分配但未初始化的存储空间的指针
args - 传递给 T 构造函数的构造参数
x - 传递给 T1 构造函数的构造参数
y - 传递给 T2 构造函数的构造参数
xy - 其两个成员分别为 T1 T2 构造参数的对
tup - 待合并的参数

注释

此函数由任何感知分配器的对象(例如 std::allocator_traits )通过 std::vector 调用,这些对象被赋予 std::scoped_allocator_adaptor 作为要使用的分配器。由于 inner_allocator_type 本身是 std::scoped_allocator_adaptor 的特化,当通过此函数构造的感知分配器对象开始构造其自身成员时,此函数也将被调用。

缺陷报告

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

缺陷报告 适用范围 发布时的行为 正确行为
LWG 2203 C++11 通过值初始化 inner_allocator_type 对象
获取内部分配器
通过调用 inner_allocator()
获取内部分配器
LWG 2511
( P0475R1 )
C++11 concat-args 可能复制 std::tuple
的元素
消除了所有元素复制操作
LWG 2586 C++11 仅检查从 inner_allocator_type
右值进行的构造
改为检查从非常量 inner_allocator_type
左值进行的构造
LWG 2975 C++11 重载 (1) 未受约束 约束为拒绝 std::pair

参见

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