C++11: Initializer lists

If you want to declare and initialize an array of five integers, you could do something like:

int a[] = { 10, 20, 5, 45, 22 };

But in C++03, what would happen if you would want to do the same thing using some STL container, like a std::vector? Something like:

std::vector<int> a = { 10, 20, 5, 45, 22 };

You guessed it!!! It would not get compiled. Initializer lists were just available for built-in data types…. Until now! :)

With C++0x, the code shown above is perfectly legal and works as expected; that is because C++0x introduces support for initializer lists for every data type.

So, the next question would be… How can I make my own container to support these initializer lists?

The answer: The STL library that will ship with C++0x includes a new template class called: std::initializer_list; when the compiler finds a declaration with an initialization similar to the one shown above { 1, 2, 3, 4, 5 };, it looks for a constructor that receives an std::initializer_list as argument. If it is found, the compiler will create an instance of the initializer_list class with the arguments inside the curly brackets and will invoke the constructor found.

The initializer list object can be iterated using a standard STL iterator.

So, let’s consider this wrapper to vector that implements the std::initializer list:

#include <vector>
#include <initializer_list>

using namespace std;

template <typename T>
class MyList
    vector<T> _items;

    MyList() { }

    MyList(const initializer_list& x)
      for (auto i = x.begin(); i != end(); i++)

With this implementation we will be able to create an instance of MyList in this way:

MyList<int> x = { 10, 20, 1, 4, 8, 16, 9 };
MyList<string> y = { "hello", "world", "of", "c++", "programmers" };

3 thoughts on “C++11: Initializer lists

  1. I really enjoy your C++11 articles :)
    This one contains, however, a minor mistake. In C++03, initializer lists were available for ‘aggregate’ types, not only built-ones. ‘Aggregate type’ is an array, or a class satysfying certain restrictions (has no user-declared ctor, no non-public non-static data, no base classes and no virtual functions).

      1. The most significant difference between these two is that POD’s member types are severely restricted, while aggregate can contain pretty much anything.
        Precisely, POD struct/union is an aggregate satisfying following conditions:
        – has no user-defined copy-assigmnent operator
        – has no user-defined destructor
        – has no non-static member being non-POD, array-of-non-POD or reference

        For longer description of these concepts (aggregate, POD) see: http://stackoverflow.com/questions/4178175/what-are-aggregates-and-pods-and-how-why-are-they-special/4178176#4178176
        (this post is not mine)

        Note: these are C++03 definitions, current (C++11) definitions are slightly altered to accomodate new concepts.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s