Cub*_*bbi 12 c++ allocator c++11
std::scoped_allocator_adaptor到目前为止,在尝试使用gcc 4.7.0中实现的C++ 11时,我注意到C++ 11 FDIS定义了std::uses_allocatorfor tuples(20.4.2.8[tuple.traits])的特化,但是没有定义对,尽管出于所有其他目的,对看起来和行为就像元组(他们的专长std::get,std::tuple_size等等).
在进一步阅读中,介绍这些东西的N2554也定义了对的allocator_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
Run Code Online (Sandbox Code Playgroud)
原因之一是我们希望避免像 std::pair 这样看似简单的类有 15 个构造函数(如N3000中的构造函数)。
我们现在有了一个“通用”构造函数
template <class... Args1, class... Args2>
pair(piecewise_construct_t,
tuple<Args1...> first_args, tuple<Args2...> second_args);
Run Code Online (Sandbox Code Playgroud)
您可以将任何您喜欢的内容传递给每个对成员的构造函数,包括分配器。
| 归档时间: |
|
| 查看次数: |
697 次 |
| 最近记录: |