Uniform Initialization

C++11 extends the applicability of the brace-enclosed list (list-initialization) so that it can be used with all built-in types and with user-defined types (that is, class objects). The list can be used either with or without the = sign:

int x = {5};double y {2.75};short quar[5] {4,5,2,76,1};

Also the list-initialization syntax can be used in new expressions:

int * ar = new int [4] {2,4,6,7};         // C++11

With class objects, a braced list can be used instead of a parenthesized list to invoke a constructor:

class Stump{private:    int roots;    double weight;public:     Stump(int r, double w) : roots(r), weight(w) {}};Stump s1(3,15.6);      // old styleStump s2{5, 43.4};     // C++11Stump s3 = {4, 32.1};  // C++11

Get C++ Primer Plus now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.