名前空間
変種
操作

std::allocator

提供: cppreference.com
< cpp‎ | memory
 
 
動的メモリ管理
スマートポインタ
(C++11)
(C++11)
(C++11)
(C++17未満)
(C++11)
アロケータ
allocator
メモリリソース
未初期化記憶域
ガベージコレクションサポート
その他
(C++20)
(C++11)
(C++11)
C のライブラリ
低水準のメモリ管理
 
std::allocator
 
ヘッダ <memory> で定義
template< class T >
struct allocator;
(1)
template<>
struct allocator<void>;
(2) (C++17で非推奨)
(C++20で削除)

std::allocator クラステンプレートは、ユーザ指定のアロケータが提供されない場合にすべての標準ライブラリコンテナによって使用される、デフォルトの Allocator です。 デフォルトのアロケータはステートレスです。 つまり、アロケータのすべてのインスタンスは交換可能であり、等しいものとして比較され、��じ型の他のインスタンスによって確保されたメモリを解放できます。

void に対する明示的特殊化には、メンバ型 referenceconst_referencesize_type および difference_type がありません。 この特殊化はメンバ関数を宣言しません。

(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

[編集] メンバ関数

新しいアロケータのインスタンスを作成します
(パブリックメンバ関数) [edit]
アロケータのインスタンスを破棄します
(パブリックメンバ関数) [edit]
(C++17で非推奨)(C++20で削除)
operator& がオーバーロードされている場合でも、オブジェクトのアドレスを取得します
(パブリックメンバ関数) [edit]
未初期化記憶域を確保します
(パブリックメンバ関数) [edit]
記憶域を解放します
(パブリックメンバ関数) [edit]
(C++17で非推奨)(C++20で削除)
サポートされている最大確保サイズを返します
(パブリックメンバ関数) [edit]
(C++17で非推奨)(C++20で削除)
確保された記憶域にオブジェクトを構築します
(パブリックメンバ関数) [edit]
(C++17で非推奨)(C++20で削除)
確保された記憶域内のオブジェクトを破棄します
(パブリックメンバ関数) [edit]

[編集] 非メンバ関数

(C++20で削除)
2つのアロケータインスタンスを比較します
(パブリックメンバ関数) [edit]

[編集] ノート

メンバテンプレートクラス 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以上)

[編集]

#include <memory>
#include <iostream>
#include <string>
 
int main()
{
    std::allocator<int> a1;   // default allocator for ints
    int* a = a1.allocate(1);  // space for one int
    a1.construct(a, 7);       // construct the int
    std::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_traits
    std::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

[編集] 関連項目

アロケータ型に関する情報を提供します
(クラステンプレート) [edit]
多段コンテナのための多段アロケータを実装します
(クラステンプレート) [edit]
指定された型がアロケータ使用構築をサポートしているかどうか調べます
(クラステンプレート) [edit]