Namespaces
Variants

std:: allocator

From cppreference.net
Memory management library
( exposition only* )
Allocators
Uninitialized memory algorithms
Constrained uninitialized memory algorithms
Memory resources
Uninitialized storage (until C++20)
( until C++20* )
( until C++20* )
( until C++20* )

Garbage collector support (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
ヘッダーで定義 <memory>
template < class T >
struct allocator ;
(1)
template <>
struct allocator < void > ;
(2) (C++17で非推奨)
(C++20で削除)

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

void の明示的特殊化には、メンバー型定義 reference const_reference size_type および difference_type が欠如している。この特殊化はメンバー関数を宣言しない。

(C++20まで)

デフォルトアロケータは アロケータ完全性要件 を満たす。

(C++17以降)

目次

翻訳の説明: - 「Contents」を「目次」に翻訳しました - C++関連の専門用語(Member types、Member functions、Non-member functions、Notes、Example、Defect reports、See also)は原文のまま保持しました - HTMLタグ、属性、クラス名、IDなどは完全に保持されています - 数値や構造は変更していません

メンバー型

定義
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
{
typedef allocator < U > other ;
} ;

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)
要求されたサイズ以上の初期化されていないストレージを割り当てる
(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 が提供された

関連項目

アロケータ型に関する情報を提供する
(クラステンプレート)
多階層コンテナのための多階層アロケータを実装する
(クラステンプレート)
指定された型がuses-allocator構築をサポートするかどうかをチェックする
(クラステンプレート)