名稱空間
變體
操作

std::scoped_allocator_adaptor<OuterAlloc,InnerAlloc...>::construct

來自 cppreference.com
 
 
記憶體管理庫
(僅作說明*)
未初始化記憶體演算法
(C++17)
(C++17)
(C++17)
受約束的未初始化
記憶體演算法
C 庫

分配器
記憶體資源
垃圾回收支援
(C++11)(直到 C++23)
(C++11)(直到 C++23)
(C++11)(直到 C++23)
(C++11)(直到 C++23)
(C++11)(直到 C++23)
(C++11)(直到 C++23)
未初始化儲存
(直到 C++20*)
(直到 C++20*)
顯式生命週期管理
 
 
定義於標頭檔案 <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) 透過 uses-allocator construction 在 `p` 指示的未初始化記憶體位置使用最外層分配器構造型別為 T 的物件。

給定 std::uses_allocator<T, inner_allocator_type>::valueuses_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) 透過 uses-allocator construction 在 `p` 指示的未初始化記憶體位置使用最外層分配器構造 std::pair 物件。
2)xprimeconcat-args <T1>(std::move(x))yprimeconcat-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));
,其中 xargyarg 定義如下:
 過載  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 型別物件所需由 uses-allocator construction 額外引數。
給定 std::uses_allocator<T, inner_allocator_type>::valueuses_inner

目錄

[編輯] 引數

p - 指向已分配但未初始化的儲存的指標
args - 要傳遞給 T 建構函式的建構函式引數
x - 要傳遞給 T1 建構函式的建構函式引數
y - 要傳遞給 T2 建構函式的建構函式引數
xy - 其兩個成員為 T1T2 建構函式引數的 pair
tup - 要合併的引數

[編輯] 注意

此函式由任何分配器感知物件(例如 std::vector)透過 std::allocator_traits 呼叫,該物件已將 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 右值進行的構造
改為檢查從非 const
inner_allocator_type 左值進行的構造
LWG 2975 C++11 過載 (1) 未受限制 限制為拒絕 std::pair

[編輯] 參閱

[靜態]
在已分配的儲存中構造一個物件
(函式模板) [編輯]
(C++20 前)
在已分配的儲存中構造物件
(std::allocator<T> 的公開成員函式) [編輯]