6

Before c++17, if you have an allocator like Allocator<typename, size_t> you can use the rebind struct. But now, in C++17 the rebind struct is deprecated. What's the solution to construct an allocator<T,size_t> from an allocator<T2, size_t>?

dj4567
  • 73
  • 7
  • The standard never has any allocator like `Allocator`. If a third party allocator has a non-type template parameter, then it must have a `rebind` member, or it will not meet the allocator requirements. – cpplearner Jan 08 '19 at 13:02
  • it must have a rebind member also in C++17? Is it not deprecated? – dj4567 Jan 08 '19 at 13:05

2 Answers2

8

Only std::allocator's rebind member template is deprecated. If you are using your own class, you can still define rebind.

Do it through std::allocator_traits, like:

using AllocatorForU = std::allocator_traits<AllocatorForT>::template rebind_alloc<U>;

The default for rebind_alloc for AllocatorTemplate<T, OtherTypes...> is AllocatorTemplate<U, OtherTypes...>, which works for std::allocator, which is why std::allocator<T>::rebind is deprecated. You have to define it for your class since it has a non-type template parameter.

Artyer
  • 31,034
  • 3
  • 47
  • 75
2

You might use std::allocator_traits:

std::allocator_traits<Alloc>::rebind_alloc<T>

with potential typename/template.

Jarod42
  • 203,559
  • 14
  • 181
  • 302