如何避免在allocator <T,N> c ++ 17中重新绑定

dj4*_*567 4 c++ allocator

在c ++ 17之前,如果您有类似的分配器,则Allocator<typename, size_t>可以使用rebind结构。但是现在,在C ++ 17中,不推荐使用rebind结构。从中构造an的解决方案allocator<T,size_t>allocator<T2, size_t>什么?

Art*_*yer 5

只有std::allocatorrebind成员模板已被弃用。如果您使用自己的课程,则仍然可以定义rebind

通过完成std::allocator_traits,例如:

using AllocatorForU = std::allocator_traits<AllocatorForT>::template rebind_alloc<U>;
Run Code Online (Sandbox Code Playgroud)

默认为rebind_allocAllocatorTemplate<T, OtherTypes...>IS AllocatorTemplate<U, OtherTypes...>,它适用于std::allocator,这就是为什么std::allocator<T>::rebind已经过时了。您必须为您的类定义它,因为它具有非类型模板参数。