std::scoped_allocator_adaptor<OuterAlloc,InnerAlloc...>::construct
来自cppreference.com
< cpp | memory | scoped allocator adaptor
在标头 <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, | (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 前) |
(5) | (C++20 前) | |
(6) | (C++20 前) | |
辅助函数模板 | ||
template<class T, class... Args> std::tuple</* 见下文 */>/*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:
此重载只有在 | (C++20 前) |
等价于 std::apply | (C++20 起) |
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 定义如下:
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) |
给定 std::uses_allocator<T, inner_allocator_type>::value 为 uses_inner:
- 如果 uses_inner 是 false 并且 std::is_constructible<T, Args...>::value 是 true,那么就会返回 std::tuple<Args&&...>(std::move(tup))。
- 否则,如果 uses_inner 和 std::is_constructible<T, std::allocator_arg_t,
inner_allocator_type&,
Args...>::value 都是 true,那么就会返回 std::tuple_cat(std::tuple<std::allocator_arg_t, inner_allocator_type&>
(std::allocator_arg, inner_allocator()),
std::tuple<Args&&...>(std::move(tup)))。 - 否则,如果 uses_inner 和 std::is_constructible<T, Args..., inner_allocator_type&>::value 都是 true,那么就会返回 std::tuple_cat(std::tuple<Args&&...>(std::move(tup)),
std::tuple<inner_allocator_type&>(inner_allocator())。 - 否则程序非良构。
目录 |
[编辑]参数
p | - | 指向被分配但未初始化的存储的指针 |
args | - | 传递给 T 的构造函数的各构造函数实参 |
x | - | 传递给 T1 的构造函数的构造函数实参 |
y | - | 传递给 T2 的构造函数的构造函数实参 |
xy | - | 两个成员分别为 T1 与 T2 构造函数实参的 pair |
non_pair | - | 转换成 pair 以进行进一步构造的非 pair 实参 |
tup | - | 要被合并的实参 |
[编辑]注解
此函数由任何具分配器对象,例如 std::vector,在给予了 std::scoped_allocator_adaptor 作为所用分配器时(通过 std::allocator_traits)调用。因为 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 的非 const 左值进行的构造 |
LWG 2975 | C++11 | 重载 (1) 没有被约束 | 添加约束以拒绝 std::pair |
[编辑]参阅
[静态] | 在已分配存储中构造对象 (函数模板) |
(C++20 前) | 在分配的存储中构造对象 ( std::allocator<T> 的公开成员函数) |