Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why was the constructor interface of std::vector changed with C++11?

Tags:

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()); 
like image 932
Christian Ammer Avatar asked Jan 20 '12 09:01

Christian Ammer


1 Answers

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 Ts. 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.

like image 68
R. Martinho Fernandes Avatar answered Sep 19 '22 15:09

R. Martinho Fernandes