为什么使用新标准删除了默认参数?我经常构造一个这样的矢量变量:std::vector<my_pod_struct> buf(100).我想我会用C++ 11编译器得到编译器错误.
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());
Run Code Online (Sandbox Code Playgroud)
R. *_*des 50
在编写之前,std::vector<T> buf(100);您将获得一个T默认构造,然后该实例将被复制到向量中的一百个插槽.
现在,当你写作时std::vector<T> buf(100);,它将使用另一个构造函数:explicit vector( size_type count );.这将默认 - 构建一百T秒.这是一个微小的差异,但是很重要.
新的单参数构造函数不要求类型T可复制.这很重要,因为现在的类型可以移动而不是可复制的.