Program Notes

You can pass an initializer_list object by value or by reference, as shown by sum() and average(). The object itself is small, typically two pointers (one to the beginning and one past end) or a pointer to the beginning and an integer representing the size, so the choice is not a major performance issue. (The STL passes them by value.)

The function argument can be a list literal, like {2,3,4}, or it can be a list variable, like dl.

The iterator types for initializer_list are const, so you can’t change the values in a list:

*dl.begin() = 2011.6;                // not allowed

But, as Listing 16.22 shows, you can attach a list variable to a different list:

dl = {16.0, 25.0, 36.0, 40.0, 64.0};  // allowed

However, the intended use of ...

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.