C++17 STL Cookbook
上QQ阅读APP看书,第一时间看更新

How to do it...

Variables are initialized in one step. Using the initializer syntax, there are two different situations:

  • Using the brace initializer syntax without auto type deduction:
       // Three identical ways to initialize an int:
int x1 = 1;
int x2 {1};
int x3 (1);

std::vector<int> v1 {1, 2, 3}; // Vector with three ints: 1, 2, 3
std::vector<int> v2 = {1, 2, 3}; // same here
std::vector<int> v3 (10, 20); // Vector with 10 ints,
// each have value 20
  • Using the brace initializer syntax with auto type deduction:
 auto v {1}; // v is int
auto w {1, 2}; // error: only single elements in direct
// auto initialization allowed! (this is new)
auto x = {1}; // x is std::initializer_list<int>
auto y = {1, 2}; // y is std::initializer_list<int>
auto z = {1, 2, 3.0}; // error: Cannot deduce element type