名稱空間
變體
操作

std::pmr::polymorphic_allocator

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

分配器
pmr::polymorphic_allocator
(C++17)
記憶體資源
垃圾回收支援
(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*)
顯式生命週期管理
 
 
定義於標頭檔案 <memory_resource>
template< class T >
class polymorphic_allocator;
(C++17 起)
(C++20 前)
template< class T = std::byte >
class polymorphic_allocator;
(C++20 起)

類模板 std::pmr::polymorphic_allocator 是一個 分配器 (Allocator),它根據構造時所用的 std::pmr::memory_resource 而展現不同的分配行為。由於 memory_resource 使用執行時多型來管理分配,因此以 polymorphic_allocator 作為其靜態分配器型別的不同容器例項可以互操作,但行為上可能像擁有不同的分配器型別。

polymorphic_allocator 的所有特化都滿足 分配器完整性要求

polymorphic_allocator::construct 成員函式執行使用分配器構造,因此使用 polymorphic_allocator 的容器的元素將為其自己的分配使用相同的分配器。例如,一個 std::pmr::vector<std::pmr::string> 將為 vector 的儲存和每個 string 的儲存使用相同的 memory_resource

對於非多型分配器,可以使用 std::scoped_allocator_adaptor 實現類似的傳播。

目錄

[編輯] 成員型別

成員型別 定義
value_type T

[編輯] 成員函式

構造 polymorphic_allocator
(公開成員函式) [編輯]
(解構函式)
(隱式宣告)
隱式宣告的解構函式
(公開成員函式) [編輯]
operator=
[已刪除]
複製賦值運算子被刪除
(公開成員函式) [編輯]
公開成員函式
分配記憶體
(公開成員函式) [編輯]
釋放記憶體
(公開成員函式) [編輯]
在已分配的儲存中構造物件
(公開成員函式) [編輯]
(C++20 中已棄用)(C++26 中恢復)
銷燬已分配儲存中的物件
(公開成員函式) [編輯]
從底層資源分配原始對齊記憶體
(公開成員函式) [編輯]
釋放從 allocate_bytes 獲取的原始記憶體
(公開成員函式) [編輯]
分配適合物件或陣列的原始記憶體
(公開成員函式) [編輯]
釋放由 allocate_object 獲取的原始記憶體
(公開成員函式) [編輯]
分配並構造一個物件
(公開成員函式) [編輯]
銷燬並釋放一個物件
(公開成員函式) [編輯]
為容器的複製建構函式建立一個新的 polymorphic_allocator
(公開成員函式) [編輯]
返回指向底層記憶體資源的指標
(公開成員函式) [編輯]

[編輯] 非成員函式

(在 C++20 中移除)
比較兩個 polymorphic_allocator
(函式) [編輯]

[編輯] 註解

polymorphic_allocator 不會在容器複製賦值、移動賦值或交換時傳播。因此,使用 polymorphic_allocator 的容器的移動賦值可能丟擲異常,並且交換兩個分配器不相等的 polymorphic_allocator 容器會導致未定義行為。

特性測試 標準 特性
__cpp_lib_polymorphic_allocator 201902L (C++20) std::pmr::polymorphic_allocator<> 作為詞彙型別

[編輯] 參閱

封裝記憶體資源的類的抽象介面
(類) [編輯]