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
解决方法
I am using gcc 4.8 with C++11.
然后你需要在你的分配器中定义重新绑定,GCC的basic_string在版本5.1之前不支持C 11分配器要求(然后只支持新的ABI字符串,即std :: __ cxx :: basic_string).
因此,您的分配器必须满足C 03分配器要求,定义所有成员,因为4.8中的字符串不使用allocator_traits
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。