Suppose you have a Container which internally uses other standard containers to form more complex data structures. Thankfully, the standard containers are already designed to do all the necessary work to ensure allocators are copied/assigned etc.
So, normally if we have some Container c
, and internally it has an std::vector<int>
, we can write a copy-assignment operator that just says:
Container& operator = (const Container& c) { m_vec = c.m_vec; return *this; }
In fact we don't even have to write that (since it's just what the default copy assignment operator does), but let's just say that in this case there's some extra required logic that the default operator wouldn't do:
Container& operator = (const Container& c)
{
/* some other stuff... */
m_vec = c.m_vec;
return *this;
}
So, in this case there's no problem because the vector assignment operator does all the work for us to make sure that allocators are properly copied-on-assignment or not copied.
But... what if we have a vector which we can't simply copy-assign. Suppose it is a vector of pointers to some other internal structure.
Say we have an internal vector which holds pointers: std::vector<node*, Alloc>
So, normally in our copy-assignment operator, we'd have to say:
Container& operator = (const Container& other)
{
vector<node*, Alloc>::allocator_type alloc = m_vec.get_allocator();
for (auto it = m_vec.begin(); it != m_vec.end(); ++it) alloc.deallocate(*it);
m_vec.clear();
for (auto it = other.m_vec.begin(); it != other.m_vec.end(); ++it)
{
node* n = alloc.allocate(1); // this is wrong, we might need to use other.get_allocator() here!
alloc.construct(n, *(*it));
m_vec.push_back(n);
}
return *this;
}
So in the above example, we need to manually deallocate all the node
objects in m_vec
, and then construct new node objects from the RHS container. (Note that I'm using the same allocator object the vector uses internally in order to allocate node objects.)
But if we want to be standards compliant here and AllocatorAware, we need to check if allocator_traits<std::vector<node*, Alloc>::allocator_type>
sets propagate_on_container_copy_assign
to true. If it does, we need to use the other container's allocator to construct the copied nodes.
But... our container type Container
doesn't use it's own allocator. It just uses an internal std::vector
... so how can we tell our internal std::vector
instance to use a copied allocator if necessary? The vector doesn't have something like a "use_allocator" or "set_allocator" member function.
So, the only thing I came up with is something like:
if (std::allocator_traits<Alloc>::propagate_on_container_copy_assignment::value)
{
m_vec = std::vector<node*, Alloc>(other.get_allocator());
}
...and then we could construct our nodes with the return value of m_vec.get_allocator();
Is this a valid idiom for creating an allocator aware container that doesn't keep it's own allocator, but rather defers to an internal standard container?
deallocate
is missing the size argument (presumably 1), but the allocator has the wrongvalue_type
(node*
instead ofnode
) andrebind
is necessary (as shown by Howard Hinnant). Fortunately, this code does not compile, but it goes to show that bad calls and UB are not far away when dealing with allocators directly.