名前空間
変種
操作

std::scoped_allocator_adaptor

From cppreference.com
< cpp‎ | memory
 
 
メモリ管理ライブラリ
(説明用*)
未初期化メモリのアルゴリズム
(C++17)
(C++17)
(C++17)
制約付き未初期化
メモリアルゴリズム
Cライブラリ

アロケータ
scoped_allocator_adaptor
(C++11)
メモリリソース
ガベージコレクションのサポート
(C++11)(C++23まで)
(C++11)(C++23まで)
(C++11)(C++23まで)
(C++11)(C++23まで)
(C++11)(C++23まで)
(C++11)(C++23まで)
未初期化ストレージ
(C++20まで*)
(C++20まで*)
明示的な生存期間管理
 
 
ヘッダ <scoped_allocator> で定義
template< class OuterAlloc, class... InnerAllocs >

class scoped_allocator_adaptor

    : public OuterAlloc;
(C++11以降)

std::scoped_allocator_adaptor クラステンプレートは、多層コンテナ (マップのタプルのリストのセットのベクトルなど) で使用できるアロケータです。1つの外側アロケータ型 OuterAlloc と、0個以上の内側アロケータ型 InnerAlloc... でインスタンス化されます。scoped_allocator_adaptor を使用して直接構築されたコンテナは、OuterAlloc を使用して要素を割り当てますが、要素自体がコンテナである場合は、最初の内側アロケータを使用します。そのコンテナの要素がそれ自体コンテナである場合は、2番目の内側アロケータを使用し、以下同様です。コンテナのレベルが内側アロケータの数よりも多い場合、最後の内側アロケータが、それ以降のすべてのネストされたコンテナで再利用されます。

このアダプタの目的は、ネストされたコンテナ内のステートフルなアロケータを正しく初期化することです。たとえば、ネストされたコンテナのすべてのレベルを同じ共有メモリーセグメントに配置する必要がある場合などです。アダプタのコンストラクタは、リスト内のすべてのアロケータの引数を受け取り、各ネストされたコンテナは、必要に応じてアダプタからアロケータの状態を取得します。

scoped_allocator_adaptor の目的のため、次の内側アロケータが A である場合、std::uses_allocator<T,A>::value == true であるすべてのクラス T は、コンテナであるかのように再帰に参加します。さらに、scoped_allocator_adaptor::construct の特定のオーバーロードによって、std::pair もそのようなコンテナとして扱われます。

典型的な実装では、std::scoped_allocator_adaptor<InnerAllocs...> のインスタンスをメンバオブジェクトとして保持します。

std::pmr::polymorphic_allocator は、uses-allocator構築に従ってネストされたコンテナに伝播し、std::scoped_allocator_adaptor を必要としません (また、動作しません)。

目次

[編集] 入れ子型

定義
outer_allocator_type OuterAlloc
inner_allocator_type
  • scoped_allocator_adaptor<OuterAlloc> (もし sizeof...(InnerAllocs) がゼロの場合)
  • scoped_allocator_adaptor<InnerAllocs...> (それ以外の場合)
value_type std::allocator_traits<OuterAlloc>::value_type
size_type std::allocator_traits<OuterAlloc>::size_type
difference_type std::allocator_traits<OuterAlloc>::difference_type
pointer std::allocator_traits<OuterAlloc>::pointer
const_pointer std::allocator_traits<OuterAlloc>::const_pointer
void_pointer std::allocator_traits<OuterAlloc>::void_pointer
const_void_pointer std::allocator_traits<OuterAlloc>::const_void_pointer


OuterAllocInnerAlloc... のセットを Allocs とした場合

定義
propagate_on_container_copy_assignment
propagate_on_container_move_assignment
propagate_on_container_swap
is_always_equal

[編集] メンバ関数

新しい scoped_allocator_adaptor オブジェクトを作成する
(public メンバ関数) [編集]
scoped_allocator_adaptor オブジェクトを破棄する
(public メンバ関数) [編集]
scoped_allocator_adaptor を代入する
(public メンバ関数) [編集]
inner_allocator 参照を取得する
(public メンバ関数) [編集]
outer_allocator 参照を取得する
(public メンバ関数) [編集]
外側のアロケータを使用して未初期化のストレージを割り当てる
(public メンバ関数) [編集]
外側アロケータを使用してストレージを解放する
(public メンバ関数) [編集]
外側アロケータがサポートする最大の割り当てサイズを返す
(public メンバ関数) [編集]
割り当てられたストレージにオブジェクトを構築し、適切であれば内側アロケータをそのコンストラクタに渡す
(public メンバ関数) [編集]
割り当てられたストレージにあるオブジェクトを破棄する
(public メンバ関数) [編集]
scoped_allocator_adaptor とそのすべてのアロケータの状態をコピーする
(public メンバ関数) [編集]
説明専用関数テンプレート
最も外側のアロケータを取得する
(説明専用メンバ関数*)
最も外側のアロケータを使用してオブジェクトを構築する
(説明専用メンバ関数*)
最も外側のアロケータを使用してオブジェクトを破棄する
(説明専用メンバ関数*)

[編集] 非メンバ関数

(C++20で削除)
2つの scoped_allocator_adaptor オブジェクトを比較する
(関数テンプレート) [編集]

[編集] 推論ガイド(C++17以降)

[編集] 入れ子クラス

Class 定義
rebind  template< class T >

struct rebind
{
    using other = scoped_allocator_adaptor
                      <std::allocator_traits<OuterAlloc>::template rebind_alloc<T>,
                       InnerAllocs...>;
};

[編集]

#include <boost/interprocess/allocators/adaptive_pool.hpp>
#include <boost/interprocess/managed_shared_memory.hpp>
#include <scoped_allocator>
#include <vector>
 
namespace bi = boost::interprocess;
 
template<class T>
using alloc = bi::adaptive_pool<T, bi::managed_shared_memory::segment_manager>;
 
using ipc_row = std::vector<int, alloc<int>>;
 
using ipc_matrix = std::vector<ipc_row, std::scoped_allocator_adaptor<alloc<ipc_row>>>;
 
int main()
{
    bi::managed_shared_memory s(bi::create_only, "Demo", 65536);
 
    // create vector of vectors in shared memory
    ipc_matrix v(s.get_segment_manager());
 
    // for all these additions, the inner vectors obtain their allocator arguments
    // from the outer vector's scoped_allocator_adaptor
    v.resize(1);
    v[0].push_back(1);
    v.emplace_back(2);
    std::vector<int> local_row = {1, 2, 3};
    v.emplace_back(local_row.begin(), local_row.end());
 
    bi::shared_memory_object::remove("Demo");
}

[編集] 欠陥報告

以下の動作変更を伴う欠陥報告が、以前に公開されたC++標準に遡って適用されました。

DR 適用対象 公開された動作 正しい動作
LWG 2108 C++11 scoped_allocator_adaptor がステートレスであるかどうかを示す方法がなかった is_always_equal を提供した

[編集] 関連項目

アロケータ型に関する情報を提供します
(クラステンプレート) [編集]
指定された型が uses-allocator 構築をサポートしているかどうかをチェックします
(クラステンプレート) [編集]
デフォルトアロケータ
(クラステンプレート) [編集]
English 日本語 中文(简体) 中文(繁體)