Why was the default argument removed with the new standard? Often I constructed a vector variable like this: std::vector<my_pod_struct> buf(100)
. I guess I would get an compiler error with a C++11 compiler.
explicit vector( size_type count, const T& value = T(), /* until C++11 */ const Allocator& alloc = Allocator()); vector( size_type count, const T& value, /* since C++11 */ const Allocator& alloc = Allocator());
Before, when you wrote std::vector<T> buf(100);
you would get one T
default constructed, and then that instance would be copied over to one hundred slots in the vector.
Now, when you write std::vector<T> buf(100);
, it will use another constructor: explicit vector( size_type count );
. This will default-construct one hundred T
s. It's a slight difference, but an important one.
The new single-argument constructor doesn't require the type T
to be copyable. This is important because now types can be movable and not copyable.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With