scoped_allocator_adaptor(); // (1)
template <class OuterA2>
scoped_allocator_adaptor(
OuterA2&& outerAlloc,
const InnerAllocs&... innerAllocs) noexcept; // (2)
scoped_allocator_adaptor(
const scoped_allocator_adaptor& other) noexcept; // (3)
scoped_allocator_adaptor(
scoped_allocator_adaptor&& other) noexcept; // (4)
template <class OuterA2>
scoped_allocator_adaptor(
const scoped_allocator_adaptor<OuterA2, InnerAllocs...>& other) noexcept; // (5)
template <class OuterA2>
scoped_allocator_adaptor(
scoped_allocator_adaptor<OuterA2, InnerAllocs...>&& other) noexcept; // (6)
scoped_allocator_adaptorオブジェクトの構築
- (1) : デフォルト構築。各アロケータオブジェクトを値初期化する。
- (2) : 外側のアロケータ、内側のアロケータという順番で、各アロケータオブジェクトを受け取る。
- (3) : コピー構築。
otherが持つアロケータオブジェクトを*thisにコピーする。 - (4) : ムーブ構築。
otherが持つアロケータオブジェクトを*thisにムーブする。 - (5) : 変換可能な外側のアロケータを持つ
scoped_allocator_adaptorオブジェクトからのコピー構築。 - (6) : 変換可能な外側のアロケータを持つ
scoped_allocator_adaptorオブジェクトからのムーブ構築。
テンプレートパラメータ制約
- (2) :
- C++11 : クラステンプレートのパラメータ
OuterAllocが、テンプレートパラメータの型OuterA2から構築可能であること - C++17 :
is_constructible_v<OuterAlloc, OuterA2>がtrueであること
- C++11 : クラステンプレートのパラメータ
- (5) :
- C++11 :
OuterAllocがOuterA2から構築可能であること - C++17 :
is_constructible_v<OuterAlloc, const OuterA2&>がtrueであること
- C++11 :
- (6) :
- C++11 :
OuterAllocがOuterA2から構築可能であること - C++17 :
is_constructible_v<OuterAlloc, OuterA2>がtrueであること
- C++11 :
効果
- (1) : 外側と内側のアロケータオブジェクトを値初期化する。
- (2) : 外側のアロケータを
std::forward<OuterA2>(outerAlloc)でムーブ構築する。内側のアロケータオブジェクトはコピー構築する。 - (3) :
otherが持つアロケータオブジェクトを、*thisの対応する各アロケータオブジェクトにコピーする。 - (4) :
otherが持つアロケータオブジェクトを、*thisの対応する各アロケータオブジェクトにムーブする。 - (5) :
otherが持つアロケータオブジェクトを、*thisの対応する各アロケータオブジェクトにコピーする。 - (6) :
otherが持つアロケータオブジェクトを、*thisの対応する各アロケータオブジェクトにムーブする。
例
#include <iostream>
#include <vector>
#include <string>
#include <scoped_allocator>
#include <utility>
template <class T>
using alloc_t = std::allocator<T>;
// コンテナの要素(Inner)
using string = std::basic_string<
char,
std::char_traits<char>,
alloc_t<char>
>;
// コンテナ(Outer)
template <class T>
using vector = std::vector<
T,
std::scoped_allocator_adaptor<alloc_t<T>, alloc_t<typename T::value_type>>
>;
int main()
{
// (1)
// デフォルト構築。
// 各アロケータを値初期化する。
vector<string>::allocator_type alloc1;
// (2)
// 外側のアロケータと内側のアロケータ、それぞれを渡す
vector<string>::allocator_type alloc2 {
alloc_t<string>(), // vector自体のアロケータオブジェクト
alloc_t<char>() // vectorの全ての要素に使用するアロケータオブジェクト
};
// (3)
// コピー構築。対応するアロケータオブジェクトにコピーする。
vector<string>::allocator_type alloc3 = alloc2;
// (4)
// ムーブ構築。対応するアロケータオブジェクトにムーブする。
vector<string>::allocator_type alloc4 = std::move(alloc3);
}
出力
バージョン
言語
- C++11
処理系
- Clang: 3.0 ✅
- GCC: 4.7.3 ✅
- ICC: ??
- Visual C++: ??
参照
- LWG Issue 2782.
scoped_allocator_adaptorconstructors must be constrained- C++17で、(2), (5), (6)の各コンストラクタが、C++11では単なる要件だった「
OuterAllocがOuterA2から構築可能」をオーバーロード解決に参加する条件(制約)として規定するよう変更され、非互換なアロケータ型間の暗黙変換が排除された - (5)の制約は
is_constructible_v<OuterAlloc, const OuterA2&>、(2)と(6)の制約はis_constructible_v<OuterAlloc, OuterA2>である
- C++17で、(2), (5), (6)の各コンストラクタが、C++11では単なる要件だった「