如果自定义分配器中不存在重绑定,std :: allocator_traits是否定义了rebind_alloc?

Jac*_*kie 7 c++ templates allocator c++11

我正在尝试重新绑定我的自定义分配器类型MyAllocator<foo>,以便在basic_string类中使用,例如:

std::basic_string<char, std::char_traits<char>, MyAllocator<char>> ...

分配器被传递给上下文MyAllocator<void>,所以我需要重新绑定分配器.

从cppreference页面std::allocator_traits,http://en.cppreference.com/w/cpp/memory/allocator_traits :

成员别名模板:

rebind_alloc<T>: Alloc::rebind<T>::other如果存在,否则Alloc<T, Args>如果此Alloc是Alloc<U, Args>

我的自定义分配器实现allocator_traits,但没有定义重新绑定结构(这似乎不是实现的要求allocator_traits).我对文档的理解是它allocator_traits应该理解rebind_alloc.但是,如果我尝试调用rebind_alloc我的自定义分配器类型:

template<typename T>
using RebindAlloc =
  typename std::allocator_traits<MyAllocator<void>>::template rebind_alloc<T>;
Run Code Online (Sandbox Code Playgroud)

我尝试传递RebindAlloc<char>basic_string类型时遇到各种编译器错误:

In file included from /usr/bin/../lib/gcc/x86_64-linux-gnu/4.8/../../../../include/c++/4.8/string:52:
/usr/bin/../lib/gcc/x86_64-linux-gnu/4.8/../../../../include/c++/4.8/bits/basic_string.h:114:41: error: 
  'rebind' following the 'template' keyword does not refer to a template
  typedef typename _Alloc::template rebind<_CharT>::other _CharT_alloc_type;
Run Code Online (Sandbox Code Playgroud)

很明显,文档误导了我.我应该放弃rebind_alloc并在自定义分配器中实现重新绑定,还是有正确的方法来执行此操作allocator_traits

我正在使用gcc 4.8和C++ 11.14目前不是一种选择.

这是我正在尝试做的代码片段:https: //gist.github.com/jacquelinekay/0cee73d1d2d78d8edd31

Jon*_*ely 5

我正在使用gcc 4.8和C++ 11.

然后你需要rebind在你的分配器中定义,GCC basic_string不支持版本5.1之前的C++ 11分配器要求(然后只支持新的ABI字符串,即std::__cxx::basic_string).

因此,您的分配器必须满足C++ 03 Allocator要求,定义所有成员,因为allocator_traits4.8中的字符串不使用它