std :: scoped_allocator_adaptor和一个使用std :: allocator_arg_t的构造函数的类

时间:2016-01-15 11:34:44

标签: c++ c++11 stl allocator

我在这里找到一些单词http://en.cppreference.com/w/cpp/memory/scoped_allocator_adaptor/construct

  

如果std::uses_allocator<T, inner_allocator_type>::value==true(类型   T使用分配器,例如它是一个容器)

     

如果   std::is_constructible<T, std::allocator_arg_t, inner_allocator_type, Args...>::value==true

     

然后调用

std::allocator_traits<OUTERMOST>::construct( OUTERMOST(*this),
                                             p,
                                             std::allocator_arg,
                                             inner_allocator(),
                                             std::forward<Args>(args)... );

所以,我做了一个简单的测试

struct use_arg {
    template <typename Alloc>
    use_arg(std::allocator_arg_t, Alloc &, int i)
        { std::cout << i << " in use_arg()\n"; }
};

namespace std {

template <typename A> struct uses_allocator<use_arg, A>: true_type {};

} // namespace std

void test_scoped()
{
    std::scoped_allocator_adaptor<std::allocator<use_arg>> sa;
    auto p = sa.allocate(1);
    sa.construct(p, 4);
    sa.destroy(p);
    sa.deallocate(p, 1);
}

但是gcc和clang给了我这些错误https://gist.github.com/anonymous/3e72754a7615162280fb

我还写use_a来替换use_arg。它可以成功运行。

struct use_a {
    template <typename Alloc>
    use_a(int i, Alloc &) { std::cout << i << " in use_a()\n"; }
};

是什么让这些行为发生?

2 个答案:

答案 0 :(得分:3)

我认为libstdc ++和libc ++正在完全符合标准对OP示例的要求。

is_constructible<use_arg, allocator_arg_t, inner_allocator_type, int>为真,但use_arg为false,因为construct无法从rvalue分配器构造,因此struct use_arg { using allocator_type = std::allocator<use_arg>; use_arg(allocator_type&&) { } }; 调用应该是格式不正确的。

但是,我认为这是标准的缺陷。考虑这种类型:

uses_allocator

is_constructiblescoped_allocator_adaptor::construct(pointer)特征都是正确的,但对is_constructible<T, inner_allocator_type>的调用将无法编译。

检查inner_allocator_type&(从rvalue分配器测试构造)然后传递let string1 = "rain, wait, train".wordsInArray let string2 = "oil, join, coin".wordsInArray let string3 = "made, came, same".wordsInArray let lists: [String: [String]] = ["Set 1: List 1": string1, "Set 1: List 2": string2, "Set 1: List 3": string3] var index = 0 For list in lists { list.listIndex = index index = index + 1 coreDataStack.saveMainContext() } extension String { var wordsInArray:[String] { return componentsSeparatedByCharactersInSet(NSCharacterSet.punctuationCharacterSet()).joinWithSeparator("").componentsSeparatedByString(" ") } (这是一个左值)是不一致的,但这就是标准所说的。

答案 1 :(得分:1)

问题是,您通过引用收到Alloc

std::is_constructible<T, std::allocator_arg_t, inner_allocator_type, Args...>::value==true

此处您的案例inner_allocator只是std::scoped_allocator_adaptor<std::allocator<use_arg>> 它无法转换为std::scoped_allocator_adaptor<std::allocator<use_arg>>&。您可以按价值收发Alloc,也可以按const-reference收到。

相关问题