std::experimental::pmr::polymorphic_allocator<T>::construct
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, |
(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) |
(5) | (庫基礎 TS) | |
(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) 首先,如果 T1
或 T2
是分配器感知型別,則根據以下三條規則修改元組 x 和 y 以包含 this->resource()
,從而得到兩個新的元組 xprime
和 yprime
:
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),則 xprime
為 std::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),則 xprime
為 std::tuple_cat(std::move(x), std::make_tuple(this->resource()))。
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 的成員型別,如果它們接受的話。
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 | - | 其兩個成員為 T1 和 T2 建構函式引數的 pair |
[編輯] 返回值
(無)
[編輯] 注意
此函式由任何分配器感知物件(如 std::vector)呼叫(透過 std::allocator_traits),該物件被賦予 std::polymorphic_allocator 作為要使用的分配器。由於 `memory_resource*` 隱式轉換為 `polymorphic_allocator`,記憶體資源指標將傳播到任何使用多型分配器的分配器感知子物件。
[編輯] 參閱
[靜態] |
在已分配的儲存中構造一個物件 (函式模板) |
(C++20 前) |
在已分配的儲存中構造物件 (`std::allocator<T>` 的公共成員函式) |