I read this as saying that all allocators must be copy-constructible in such a way that the copies are interchangeable with the originals. Worse, the same true across type boundaries. This seems to be a pretty onerous requirement; as far as I can tell, it makes impossible a large number of types of allocators.
It is trivial to meet the requirements if allocators are a lightweight handle onto some memory resource. Just don't try to embed the resource inside individual allocator objects.
For example, say I had a freelist class that I wanted to use in my allocator, in order to cache freed objects. Unless I'm missing something, I couldn't include an instance of that class in the allocator, because the sizes or alignments of T and U might differ and therefore the freelist entries are not compatible.
[allocator.requirements] paragraph 9:
An allocator may constrain the types on which it can be instantiated and the arguments for which its construct
member may be called. If a type cannot be used with a particular allocator, the allocator class or the call to construct
may fail to instantiate.
It's OK for your allocator to refuse to allocate memory for anything except a given type T
. That will prevent it being used in node-based containers such as std::list
which need to allocate their own internal node types (not just the container's value_type
) but it will work fine for std::vector
.
That can be done by preventing the allocator being rebound to other types:
class T;
template<typename ValueType>
class Alloc {
static_assert(std::is_same<ValueType, T>::value,
"this allocator can only be used for type T");
// ...
};
std::vector<T, Alloc<T>> v; // OK
std::list<T, Alloc<T>> l; // Fails
Or you could only support types that can fit in sizeof(T)
:
template<typename ValueType>
class Alloc {
static_assert(sizeof(ValueType) <= sizeof(T),
"this allocator can only be used for types not larger than sizeof(T)");
static_assert(alignof(ValueType) <= alignof(T),
"this allocator can only be used for types with alignment not larger than alignof(T)");
// ...
};
- Are my interpretations above correct?
Not entirely.
- I've read in a few places that C++11 improved support for "stateful allocators". How is that the case, given these restrictions?
The restrictions before C++11 were even worse!
It is now clearly specified how allocators propagate between containers when copied and moved, and how various container operations behave when their allocator instance is replaced by a different instance that might not compare equal to the original. Without those clarifications it was not clear what was supposed to happen if e.g. you swapped two containers with stateful allocators.
- Do you have any suggestions for how to do the sort of thing I'm trying to do? That is, how do I include allocated-type-specific state in my allocator?
Don't embed it directly in the allocator, store it separately and have the allocator refer to it by a pointer (possibly smart pointer, depending on how you design the lifetime management of the resource). The actual allocator object should be a lightweight handle on to some external source of memory (e.g. an arena, or pool, or something managing a freelist). Allocator objects that share the same source should compare equal, this is true even for allocators with different value types (see below).
I also suggest that you don't try to support allocation for all types if you only need to support it for one.
- In general, the language around allocators seems sloppy. (For example, the prologue to Table 28 says to assume that a is of type X&, but some of the expressions redefine a.)
Yes, as you reported at https://github.com/cplusplus/draft/pull/334 (thanks).
Also, at least GCC's support is non-conformant.
It's not 100%, but will be in the next release.
What accounts for this weirdness around allocators? Is it just an infrequently used feature?
Yes. And there's a lot of historical baggage, and it's difficult to specify to be widely useful. My ACCU 2012 presentation has some details, I'll be very surprised if after reading that you think you can make it simpler ;-)
Regarding when allocators compare equal, consider:
MemoryArena m;
Alloc<T> t_alloc(&m);
Alloc<T> t_alloc_copy(t_alloc);
assert( t_alloc_copy == t_alloc ); // share same arena
Alloc<U> u_alloc(t_alloc);
assert( t_alloc == u_alloc ); // share same arena
MemoryArena m2
Alloc<T> a2(&m2);
assert( a2 != t_alloc ); // using different arenas
The meaning of allocator equality is that the objects can free each other's memory, so if you allocate some memory from t_alloc
and (t_alloc == u_alloc)
is true
, then it means you can deallocate that memory using u_alloc
. If they're not equal, u_alloc
can't deallocate memory that came from t_alloc
.
If you just have a freelist where any memory can get added to any other freelist then maybe all your allocator objects would compare equal to each other.
rebind
, when you instantiate astd::list<T, std::allocator<T>>
it will not use astd::allocator<T>
directly, but instead usestd::allocator<__list_node<T>>
internally. Thus the requirements of equivalence across type boundaries: it's necessary to insulate the user of a container from the internal way memory is managed. – Graduationrebind
, yielding an instance that's not equal. But the standard doesn't allow for that, as far as I can tell. – Cyanine