Why is there no vector(size_type n, const Allocator& alloc) in C++11?
Question
In C++11, std::vector has the constructor vector(size_type n)
which will default construct n
items in place, which can be used with default constructible, movable, non-copyable classes.
However, unlike every other vector constructor, there is no variant that takes an allocator, and I have resorted to the following:
// Foo is default constructible and moveable, but not copyable
const int n = 10; // Want 10 default constructed Foos
std::vector<Foo, CustomAllocator> foos(allocator);
foos.reserve(n);
for (int i = 0; i < n; ++i)
foos.emplace_back();
Is there a better way to accomplish this? Is there a specific reason vector(size_type n, const Allocator& alloc)
was omitted from the standard?
No correct solution
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow