相关疑难解决方法(0)

2753
推荐指数
11
解决办法
81万
查看次数

为什么采用std :: initializer_list的构造函数不是双花括号语法的首选

统一初始化是一个重要且有用的C++ 11特性.但是,您不能{}随处使用,因为:

std::vector<int> a(10, 0);    // 10 elements of value zero
std::vector<int> b({10, 0});  // 2 elements of value 10 and 0 respectively
std::vector<int> c{10, 0};    // 2 elements of value 10 and 0 respectively
std::vector<int> d = {10, 0}; // 2 elements of value 10 and 0 respectively

auto e(0);    // deduced type is int
auto f = 0;   // deduced type is int
auto g{0};    // deduced type is std::initializer_list<int>
auto h = {0}; // …
Run Code Online (Sandbox Code Playgroud)

c++ initializer-list uniform-initialization c++11

11
推荐指数
2
解决办法
4203
查看次数