std::allocator
提供: cppreference.com
ヘッダ <memory> で定義 | ||
template<class T > struct allocator; | (1) | |
template<> struct allocator<void>; | (2) | (C++17で非推奨) (C++20で削除) |
std::allocator
クラステンプレートは、ユーザ指定のアロケータが提供されない場合にすべての標準ライブラリコンテナによって使用される、デフォルトの Allocator です。 デフォルトのアロケータはステートレスです。 つまり、アロケータのすべてのインスタンスは交換可能であり、等しいものとして比較され、同じ型の他のインスタンスによって確保されたメモリを解放できます。
void に対する明示的特殊化には、メンバ型 | (C++20未満) |
カスタムアロケータもステートレスでなければなりません。 | (C++11未満) |
カスタムアロケータはステートを持っていても構いません。 それぞれのコンテナまたは他のアロケータ対応オブジェクトは、供給されたアロケータのインスタンスを格納し、 std::allocator_traits を通してアロケータの置き換えを制御します。 | (C++11以上) |
デフォルトのアロケータはアロケータの完全性の要件を満たします。 | (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++14) | std::true_type |
rebind (C++17で非推奨)(C++20で削除) | template<class U >struct rebind {typedef allocator<U> other;}; |
is_always_equal (C++17) | std::true_type |
[編集]メンバ関数
新しいアロケータのインスタンスを作成します (パブリックメンバ関数) | |
アロケータのインスタンスを破棄します (パブリックメンバ関数) | |
(C++17で非推奨)(C++20で削除) | operator& がオーバーロードされている場合でも、オブジェクトのアドレスを取得します (パブリックメンバ関数) |
未初期化記憶域を確保します (パブリックメンバ関数) | |
記憶域を解放します (パブリックメンバ関数) | |
(C++17で非推奨)(C++20で削除) | サポートされている最大確保サイズを返します (パブリックメンバ関数) |
(C++17で非推奨)(C++20で削除) | 確保された記憶域にオブジェクトを構築します (パブリックメンバ関数) |
(C++17で非推奨)(C++20で削除) | 確保された記憶域内のオブジェクトを破棄します (パブリックメンバ関数) |
[編集]非メンバ関数
(C++20で削除) | 2つのアロケータインスタンスを比較します (パブリックメンバ関数) |
[編集]ノート
メンバテンプレートクラス rebind
は異なる型のアロケータを取得する手段を提供します。 例えば、
std::list<T, A> は、何らかの内部型 Node<T> のノードを確保するために、アロケータの A::rebind<Node<T>>::other を使用します。 | (C++11未満) |
std::list<T, A> は、何らかの内部型 Node<T> のノードを確保するために、アロケータの std::allocator_traits<A>::rebind_alloc<Node<T>> を使用します。 これは、 Aが std::allocator の場合、 A::rebind<Node<T>>::other を用いて実装されます。 | (C++11以上) |
[編集]例
Run this code
#include <memory>#include <iostream>#include <string> int main(){ std::allocator<int> a1;// default allocator for intsint* a = a1.allocate(1);// space for one int a1.construct(a, 7);// construct the intstd::cout<< a[0]<<'\n'; a1.deallocate(a, 1);// deallocate space for one int // default allocator for strings std::allocator<std::string> a2; // same, but obtained by rebinding from the type of a1 decltype(a1)::rebind<std::string>::other a2_1; // same, but obtained by rebinding from the type of a1 via allocator_traitsstd::allocator_traits<decltype(a1)>::rebind_alloc<std::string> a2_2; std::string* s = a2.allocate(2);// space for 2 strings a2.construct(s, "foo"); a2.construct(s +1, "bar"); std::cout<< s[0]<<' '<< s[1]<<'\n'; a2.destroy(s); a2.destroy(s +1); a2.deallocate(s, 2);}
出力:
7 foo bar
[編集]関連項目
(C++11) | アロケータ型に関する情報を提供します (クラステンプレート) |
(C++11) | 多段コンテナのための多段アロケータを実装します (クラステンプレート) |
(C++11) | 指定された型がアロケータ使用構築をサポートしているかどうか調べます (クラステンプレート) |