2011-08-31 5 views
12

これまでgcc 4.7.0で実装されているC++ 11 std::scoped_allocator_adaptorを試していましたが、C++ 11 FDISはタプル(20.4.2.8[tuple.traits])の特殊化をstd::uses_allocatorと定義していますが、他の目的ペアは、タプルのように見え、動作します(std::getstd::tuple_sizeなどの特殊化を持っています)。なぜタプルはuses_allocatorを持っていますが、ペアはありませんか?

これらのことを紹介したN2554は、allocator_argコンストラクタと同様にペアの特殊化(23-24ページ)を定義しました。

なぜペアになったのですか?私が見ることができないそれらを使用するための別の方法がありますか、これはタプルのためにペアの非難のヒントですか?

私のテストコードはでした:

// myalloc is like std::allocator, but has a single-argument 
// constructor that takes an int, and has NO default constructor 
typedef std::vector<int, myalloc<int>> innervector_t; 
typedef std::tuple<int, innervector_t> elem_t; 
typedef std::scoped_allocator_adaptor<myalloc<elem_t>, myalloc<int>> Alloc; 
Alloc a(1,2); 
std::vector<elem_t, Alloc> v(a); 
v.resize(1);     // uses allocator #1 for elements of v 
// the following line fails to compile if pair is used instead of tuple 
// because it attempts to default-construct myalloc<int> for innervector_t 
std::get<1>(v[0]).resize(10); // uses allocator #2 for elements of innervector_t 

答えて

4

一つの理由は、我々はのstd ::ペアのような一見単​​純なクラスのために(N3000のように)15のコンストラクタを持つ避けたいということです。

現在の代わりに一つの「汎用」コンストラクタあなたはアロケータを含め、あなたが各ペアのメンバーのコンストラクタに好きなものについてだけ渡すことができ

template <class... Args1, class... Args2> 
pair(piecewise_construct_t, 
    tuple<Args1...> first_args, tuple<Args2...> second_args); 

を持っています。

+0

4.7.0の 'scoped_allocator_adaptor'の実装は不完全で(先週の時点で)、(構造体を使用する)ペアの' construct() 'のオーバーロードが欠けています。実用的な使用事例には、私が思い付くことができるだけで十分です。 – Cubbi

関連する問題