std::pmr::polymorphic_allocator<T>::construct
來自 cppreference.com
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, |
(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 前) |
(5) | (C++17 起) (C++20 前) |
|
(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<>()),也就是說,如果對的成員型別接受記憶體資源,則將其傳遞給它們。
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))) 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 建構函式引數的 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> 的公共成員函式) |