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

标签 c++ c++11 stl allocator

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

if std::uses_allocator<T, inner_allocator_type>::value==true (the type T uses allocators, e.g. it is a container)

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

then calls

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"; }
};

是什么导致这些行为发生?

最佳答案

我认为 libstdc++ 和 libc++ 都在做标准对 OP 示例的要求。

uses_allocator<use_arg, allocator<use_arg>>是真的,但是is_constructible<use_arg, allocator_arg_t, inner_allocator_type, int>是错误的,因为 use_arg不能从右值分配器构造,所以 construct call 应该是错误的。

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

struct use_arg {
  using allocator_type = std::allocator<use_arg>;
  use_arg(allocator_type&&) { }
};

uses_allocatoris_constructible traits 都是 true,但是调用 scoped_allocator_adaptor::construct(pointer)将无法编译。

检查is_constructible<T, inner_allocator_type>不一致(从右值分配器测试构造)然后通过 inner_allocator_type& (这是一个左值),但这就是标准所说的。

关于c++ - std::scoped_allocator_adaptor 和一个使用 std::allocator_arg_t 构造函数的类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34810168/

相关文章:

c++ - 无法解析类型 'std::default_random_engine'

c++11 - SFINAE 未编译

c++ - 按字母顺序排序结构数组

c++ - 为什么STL头文件没有扩展名?

c++ - 如何在 QTreeView 中绘制项目之间的连接

c++ - 一个粒子过多:生成了GL_INVALID_VALUE错误。 <start>不满足着色器存储缓冲区的最低对齐要求

c++ - 什么是域错误

c++ - 是否有与 timeGetTime() 等效的标准库?

c++ - QTabBar 下的小部件带有不需要的框架

c++ - 将文件转换为二进制 c++ studio 2012 的问题