The constructor from size_type simply calls the C constructor once and then uses its copy constructor for the rest of the elements.
Not true, since C++11 . See std :: vector :: vector documentation :
...
vector (size_type count, const T & cost, const Allocator & alloc = Allocator ()); (2)
explicit vector (size_type count, const Allocator & alloc = Allocator ()); (3)
...
And then:
...
2) Creates a container with counted copies of elements with value value.
3) Creates a container with the default value of T. instances. No copies.
...
So you need the third constructor std::vector<C>(size)
It seems that this behavior only exists with C++11 .
I cannot find a way to do this until C++11 . Since the constructor cannot do this, the option would be to create empty vectors, fallback, and then emplace_back elements. But emplace_back has since been C++11 , so ... we will go back to the square.
source share