std::allocator_traits
works its magic automatically when I provide an STL-style container with an allocator that has a single template parameter but it doesn't when I provide an STL-style container with an allocator that has two template parameters but is otherwise similar.
What do I need to do to tell std::allocator_traits
how to interact with an allocator that has more than one template parameter? Is it possible to get std::allocator_traits
to provide reasonable defaults in this case?
As an example, if I take the simple allocator Howard Hinnant provides in Allocator Boilerplate and feed it to a std::vector<>
then all is well. If I add a dummy int
parameter to the allocator
template (and make slight modifications as needed) then I get compiler errors because the compiler couldn't find rebind
, among other things.
Here's that description in code:
http://coliru.stacked-crooked.com/a/173c57264137a351
If I have to specialize std::allocator_traits
myself in this case, is there a way to still get the defaults?
std::allocator_traits
to fill in most of the details for me though. Is there any advantage to specializingstd::allocator_traits
vs completing the allocator concept without it? – Arsenaterebind
in the allocator, see my anwer. – Taratarabarrebind
. That may be the only other thing you have to not default, which would be a lot easier than specializingallocator_traits
. – SalliesallowAlloc<T, Args...>
was being matched. – Taratarabarvector
doesn't bother callingrebind
. The OP's example will likely fail with another container that must callrebind
. – Salliesallowstd::list
instead ofstd::vector
, libc++ also doesn't compile. See also this Q&A where @JonathanWakely explains libstdc++ practice of always going throughrebind
. – Taratarabarvector<int, std::allocator<char>>
is doing the client a favor. libc++ goes out of its way to notify the client asap if there is a mismatch like this between the allocator and container: github.com/llvm-mirror/libcxx/blob/master/include/… The motivation for this behavior is because program logic may depend on two independently declared containers having the same type. And if their type differs only by accidentally misdeclared allocators, best to know at compile time, asap. – Salliesallow