为什么std::vector的构造函数接口在C++11中发生了变化
Why was the constructor interface of std::vector changed with C++11?
为什么新标准删除了默认参数?我经常构造这样一个向量变量: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());
以前,当您编写std::vector<T> buf(100);
时,您将获得一个默认构造的T
,然后该实例将被复制到向量中的一百个插槽中。
现在,当您编写std::vector<T> buf(100);
时,它将使用另一个构造函数:explicit vector( size_type count );
。这将默认构造一百个T
s。这是一个微小的区别,但却是一个重要的区别。
新的单参数构造函数不要求类型T
是可复制的。这一点很重要,因为现在类型可以移动而不可复制。
您不会,现在有单独的构造函数用于您的用例:
explicit vector(size_type n);
相关文章:
- 此测试()中发生了什么意外过程?为什么总是覆盖 ch[0 1 2..]?
- 这C++代码中发生了什么C++(指数函数)
- 哪种方式更快?究竟发生了什么,我们没有看到什么?
- 我正在将一个 std::string 传递给一个 boost 函数,该函数对该类型进行常量引用,但该值发生了变化
- c++问题:给一个变量赋值后,另一个变量发生了变化
- 从"LLONG_MAX 秒"构造 std::chrono::毫秒变量时发生了什么?
- 这个片段中关于 n 在 pc[i] 中的表示发生了什么
- istringstream,num1 和 num2 在这里发生了什么?
- 究竟发生了什么,我们需要在 c++ 中双重调度/访客
- libstdc++的make_shared布局在gcc 4.x和gcc 6.x之间是否发生了变化?
- 我是否能够确定在部署一个程序后发生了什么,我在数组末尾写入?
- 为什么我的 int 在 C++ 程序中间发生了变化?
- 这个阶乘程序内部发生了什么?
- C++ - *(int**) 地址?这里发生了什么?
- C++用用户输入在循环中填充 char 数组:输入整个字符串时到底发生了什么?
- 默认初始化的含义在C 11中发生了变化
- C++11允许对非静态和非常量成员进行类内初始化.发生了什么变化
- C++11与C++98转换运算符的行为发生了变化
- 为什么std::vector的构造函数接口在C++11中发生了变化
- 'TypeInfo<char>(char *)'没有定义,但在 C++11 之前工作;发生了什么更改,如何修复错误?