C++11 Array Initialization
As Chapter 3, “Dealing with Data,” mentioned, C++11 makes the brace form of initialization (list-initialization) a universal form for all types. Arrays already use list-initialization, but the C++11 version adds a few more features.
First, you can drop the = sign when initializing an array:
double earnings[4] {1.2e4, 1.6e4, 1.1e4, 1.7e4}; // okay with C++11
Second, you can use empty braces to set all the elements to 0:
unsigned int counts[10] = {}; // all elements set to 0float balances[100] {}; // all elements set to 0
Third, as discussed in Chapter 3, list-initialization protects against narrowing:
long plifs[] = {25, 92, 3.0}; // not allowedchar slifs[4] {'h', 'i', 1122011, '\0'}; // not allowed ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access