std::scoped_allocator_adaptor
到目前为止,在使用 gcc 4.7.0 中实现的 C++11 进行试验时,我注意到 C++11 FDIS 定义了std::uses_allocator
元组 ( 20.4.2.8[tuple.traits]
) 的特化,但不用于对,尽管对于所有其他目的,对的外观和行为就像元组一样(它们有 , 等的特化std::get
)std::tuple_size
。
在进一步阅读中,介绍了这些内容的N2554allocator_arg
还定义了对的构造函数和特uses_allocator
化(第 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