std::allocator
來自 cppreference.com
定義於標頭檔案 <memory> |
||
template< class T > struct allocator; |
(1) | |
template<> struct allocator<void>; |
(2) | (C++17 中已棄用) (C++20 中移除) |
std::allocator
類模板是所有標準庫容器在未提供使用者指定分配器時使用的預設 Allocator。預設分配器是無狀態的,即給定分配器的所有例項都是可互換的,比較相等,並且可以釋放由同一分配器型別的任何其他例項分配的記憶體。
|
(C++20 前) |
預設分配器滿足分配器完整性要求。 |
(C++17 起) |
目錄 |
[編輯] 成員型別
型別 | 定義 |
value_type
|
T
|
pointer (C++17 中已棄用)(C++20 中已移除) |
T*
|
const_pointer (C++17 中已棄用)(C++20 中已移除) |
const T* |
reference (C++17 中已棄用)(C++20 中已移除) |
T&
|
const_reference (C++17 中已棄用)(C++20 中已移除) |
const T& |
size_type
|
std::size_t |
difference_type
|
std::ptrdiff_t |
propagate_on_container_move_assignment (C++11) |
std::true_type |
rebind (C++17 中已棄用)(C++20 中已移除) |
template< class U > struct rebind |
is_always_equal (C++11)(C++23 中已棄用)(C++26 中已移除) |
std::true_type |
[編輯] 成員函式
建立一個新的分配器例項 (公共成員函式) | |
銷燬分配器例項 (公共成員函式) | |
(C++20 前) |
獲取物件的地址,即使 operator& 被過載(公共成員函式) |
分配未初始化的儲存 (公共成員函式) | |
(C++23) |
分配至少與請求大小一樣大的未初始化儲存 (公共成員函式) |
釋放儲存 (公共成員函式) | |
(C++20 前) |
返回支援的最大分配大小 (公共成員函式) |
(C++20 前) |
在已分配的儲存中構造物件 (公共成員函式) |
(C++20 前) |
在已分配的儲存中銷燬物件 (公共成員函式) |
[編輯] 非成員函式
(在 C++20 中移除) |
比較兩個分配器例項 (公共成員函式) |
[編輯] 注意
成員模板類 rebind
提供了一種獲取不同型別的分配器的方法。例如,std::list<T, A> 使用分配器 A::rebind<Node<T>>::other
(直到 C++11)std::allocator_traits<A>::rebind_alloc<Node<T>> 分配某種內部型別 Node<T>
的節點,如果 A 是 std::allocator
,則它根據 A::rebind<Node<T>>::other
實現(C++11 起)。
成員型別 is_always_equal
已透過 LWG issue 3170 棄用,因為它預設將從 std::allocator
派生的自定義分配器視為始終相等。std::allocator_traits<std::allocator<T>>::is_always_equal 未被棄用,其成員常量 value
對於任何 T
都為 true
。
[編輯] 示例
執行此程式碼
#include <iostream> #include <memory> #include <string> int main() { // default allocator for ints std::allocator<int> alloc1; // demonstrating the few directly usable members static_assert(std::is_same_v<int, decltype(alloc1)::value_type>); int* p1 = alloc1.allocate(1); // space for one int alloc1.deallocate(p1, 1); // and it is gone // Even those can be used through traits though, so no need using traits_t1 = std::allocator_traits<decltype(alloc1)>; // The matching trait p1 = traits_t1::allocate(alloc1, 1); traits_t1::construct(alloc1, p1, 7); // construct the int std::cout << *p1 << '\n'; traits_t1::deallocate(alloc1, p1, 1); // deallocate space for one int // default allocator for strings std::allocator<std::string> alloc2; // matching traits using traits_t2 = std::allocator_traits<decltype(alloc2)>; // Rebinding the allocator using the trait for strings gets the same type traits_t2::rebind_alloc<std::string> alloc_ = alloc2; std::string* p2 = traits_t2::allocate(alloc2, 2); // space for 2 strings traits_t2::construct(alloc2, p2, "foo"); traits_t2::construct(alloc2, p2 + 1, "bar"); std::cout << p2[0] << ' ' << p2[1] << '\n'; traits_t2::destroy(alloc2, p2 + 1); traits_t2::destroy(alloc2, p2); traits_t2::deallocate(alloc2, p2, 2); }
輸出
7 foo bar
[編輯] 缺陷報告
下列更改行為的缺陷報告追溯地應用於以前出版的 C++ 標準。
缺陷報告 | 應用於 | 釋出時的行為 | 正確的行為 |
---|---|---|---|
LWG 2103 | C++11 | 可能需要 allocator 之間冗餘的比較 |
提供了 propagate_on_container_move_assignment |
LWG 2108 | C++11 | 無法表明 allocator 是無狀態的 |
提供 is_always_equal |
[編輯] 另請參閱
(C++11) |
提供關於分配器型別的資訊 (類模板) |
(C++11) |
為多層容器實現多層分配器 (類模板) |
(C++11) |
檢查指定型別是否支援 uses-allocator 構造 (類模板) |