std:: allocator
|
ヘッダーで定義
<memory>
|
||
|
template
<
class
T
>
struct allocator ; |
(1) | |
|
template
<>
struct allocator < void > ; |
(2) |
(C++17で非推奨)
(C++20で削除) |
std::allocator
クラステンプレートは、ユーザー指定のアロケータが提供されない場合にすべての標準ライブラリコンテナが使用するデフォルトの
Allocator
です。デフォルトアロケータはステートレスであり、つまり、与えられたアロケータのすべてのインスタンスは互換性があり、等価比較が可能で、同じアロケータ型の他のインスタンスによって割り当てられたメモリを解放できます。
|
void
の明示的特殊化には、メンバー型定義
|
(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 |
メンバー関数
|
新しいアロケータインスタンスを作成する
(public member function) |
|
|
アロケータインスタンスを破棄する
(public member function) |
|
|
(until C++20)
|
オブジェクトのアドレスを取得する(
operator
&
がオーバーロードされている場合でも)
(public member function) |
|
初期化されていないストレージを割り当てる
(public member function) |
|
|
(C++23)
|
要求されたサイズ以上の初期化されていないストレージを割り当てる
(public member function) |
|
ストレージを解放する
(public member function) |
|
|
(until C++20)
|
サポートされる最大の割り当てサイズを返す
(public member function) |
|
(until C++20)
|
割り当てられたストレージ内にオブジェクトを構築する
(public member function) |
|
(until C++20)
|
割り当てられたストレージ内のオブジェクトを破棄する
(public member function) |
非メンバー関数
|
(C++20で削除)
|
2つのアロケータインスタンスを比較する
(公開メンバ関数) |
注記
メンバーテンプレートクラス
rebind
は、異なる型に対するアロケータを取得する方法を提供します。例えば、
std::
list
<
T, A
>
は内部型
Node<T>
のノードを、アロケータ
A::rebind<Node<T>>::other
(C++11まで)
std::
allocator_traits
<
A
>
::
rebind_alloc
<
Node
<
T
>>
を使用して割り当てます。これは
A::rebind<Node<T>>::other
に基づいて実装されており、Aが
std::allocator
の場合に適用されます
(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() { // int型のデフォルトアロケータ std::allocator<int> alloc1; // 直接使用可能なメンバーの実演 static_assert(std::is_same_v<int, decltype(alloc1)::value_type>); int* p1 = alloc1.allocate(1); // int1つ分の領域 alloc1.deallocate(p1, 1); // そして解放 // これらもtraitsを通して使用可能なので、直接使用する必要はない using traits_t1 = std::allocator_traits<decltype(alloc1)>; // 対応するtrait p1 = traits_t1::allocate(alloc1, 1); traits_t1::construct(alloc1, p1, 7); // intを構築 std::cout << *p1 << '\n'; traits_t1::deallocate(alloc1, p1, 1); // int1つ分の領域を解放 // string型のデフォルトアロケータ std::allocator<std::string> alloc2; // 対応するtraits using traits_t2 = std::allocator_traits<decltype(alloc2)>; // traitを使用してstring用にアロケータをリバインドすると同じ型が得られる traits_t2::rebind_alloc<std::string> alloc_ = alloc2; std::string* p2 = traits_t2::allocate(alloc2, 2); // 2つのstring用の領域 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++規格に対して遡及的に適用されました。
| DR | 適用対象 | 公開時の動作 | 正しい動作 |
|---|---|---|---|
| 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構築をサポートするかどうかをチェックする
(クラステンプレート) |