Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Why allocator<T>::rebind<U> was removed with C++20`

20 views
Skip to first unread message

Bonita Montero

unread,
Sep 14, 2019, 1:54:33 PM9/14/19
to
Does anyone know why allocator<T>::rebind<U> was removed with C++20?
I think it's rather convenient for initializing an allocator from
a "base"-allocator, i.e. you could for example do this:

#include <memory>

using my_allocator = std::allocator<char>;
using derived_allocator = my_allocator::rebind<int>::other;
my_allocator ma;
derived_allocator da( ma );

This might not make much sense for std:allocator, but imagine you
have a class which needs allocators for different types and you
don't want to declare each allocator-type as a templae-parameter.
I need something like this for a special kind of NUMA-allocator
where there's only a single allocator-type given through a tem-
plate-parameter and all other allocators are derived from that
in the above manner.
But how can I do this with C++20?

Bonita Montero

unread,
Sep 14, 2019, 2:49:14 PM9/14/19
to
> #include <memory>
>
> using my_allocator      = std::allocator<char>;
> using derived_allocator = my_allocator::rebind<int>::other;
> my_allocator      ma;
> derived_allocator da( ma );

I've got it; now it works like this:

#include <memory>

using my_allocator = std::allocator<char>;
using derived_allocator =
std::allocator_traits<my_allocator>::rebind_alloc<int>;
my_allocator ma;
derived_allocator da( ma );

This unfortunately means that I've to specialize std::allocator_traits
for my NUMA-allocator. That's more work than simply implementing rebind
for my NUMA-allocator-class.

Bo Persson

unread,
Sep 14, 2019, 4:49:57 PM9/14/19
to
No, you don't really have to do all the extra work.

std::allocator_traits is supposed to be smart enough to use the
allocator's rebind member, if there is one. Or otherwise provide a default.

http://eel.is/c++draft/allocator.traits.types#11



Bo Persson

Bonita Montero

unread,
Sep 14, 2019, 9:00:50 PM9/14/19
to
Now I had something like this:

#include <memory>

using namespace std;

template<typename Alloc = allocator<char>>
struct S
{
struct A
{
};

using a_alloc = typename allocator_traits<Alloc>::template
rebind_alloc<A>;
};

So why is this "template" in:
using a_alloc = typename allocator_traits<Alloc>::template rebind_alloc<A>;
necessary?
0 new messages