This question is probably quite strange and one can argue that it is an XY-problem, so I'll add a section to the end to explain what's the real issue.
From the documentation for std::vector, it's obvious that one can provide an allocator when a constructor is invoked and the allocator in use is returned by the get_allocator member method.
Anyway, there is nothing like a set_allocator method that can be used to reset the allocator.
The question is: why?
Apart for the obvious performance issue, I could find myself in the need of resetting the allocator when the vector is still empty, because the default constructed one is not so good for me.
I know that for standard allocator it is said that:
Since the default allocator is stateless, the constructors have no visible effect.
But this could not be true for custom allocators.
Because of that, to copy initializale the vector's one could be a requirement.
Why doesn't exist anything like set_allocator?
As an example, imagine to have a data member defined as std::array<std::vector<int, MyAlloc>, 42> myArr. In that case, one cannot easily default initialize his vectors, so using a std::vector with a custom allocator in such a case would not have much sense, unless it guarantees that the allocator itself is stateless.
Aucun commentaire:
Enregistrer un commentaire