How to avoid rebind in allocator<T, N> c++17
Asked Answered
A

2

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>?

Artificial answered 8/1, 2019 at 12:59 Comment(2)
The standard never has any allocator like Allocator<typename, size_t>. 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.Necaise
it must have a rebind member also in C++17? Is it not deprecated?Artificial
F
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.

Fernandafernande answered 8/1, 2019 at 13:3 Comment(0)
D
2

You might use std::allocator_traits:

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

with potential typename/template.

Dearing answered 8/1, 2019 at 13:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.