C++Primer里有印象的就不赘述了
Uniform Initialization
Brace Initialization (avaliable everywhere)
- Aggregates initialized top-to-bottom/front-to-back.
- Non-aggregates initialized via constructor.
int a[] = {1,2,3,4};
struct Point {int x, y;};
const Point p {10, 20};
const Point p = {10, 20};
std::vector<int> v {1,2,3};
implicit narrowing not allowed in c++11
struct Point {int x, y};
Point p1 {1, 2.5}; // error in c++11
Point p1 {1, static_cast<int>(2.5)}; // OK
struct A {double x};
A a {1}; // OK
std::initializer_list parameters allow “initialization” lists to be passed to functions.
class A {
public:
A(std::initializer_list<int> ids) {}
};
A a {1, 2, 3, 4};