我正在尝试重新绑定我的自定义分配器类型MyAllocator< foo>,以便在basic_string类中使用,例如:
std :: basic_string< char,std :: char_traits< char> ;,MyAllocator< char>> …
分配器作为MyAllocator< void>传递给上下文,所以我需要重新绑定分配器.
从std :: allocator_traits,http://en.cppreference.com/w/cpp/memory/allocator_traits的cppreference页面:
Member alias templates:
rebind_alloc<T>
:Alloc::rebind<T>::other
if present,otherwiseAlloc<T,Args>
if this Alloc isAlloc<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>;
当我尝试传递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;
很明显,文档误导了我.我应该放弃rebind_alloc并在自定义分配器中实现重新绑定,还是使用allocator_traits有正确的方法?
我使用gcc 4.8和C 11. 14目前不是一个选项.
这是我正在尝试做的代码片段:
https://gist.github.com/jacquelinekay/0cee73d1d2d78d8edd31