2

I'm currently tasked with creating a definition for a special custom class my professor provided to us; I've never seen initializer_list in use, nor did my professor go over it. What exactly does it do?

template<class T>
LinkedQueue<T>::LinkedQueue(const std::initializer_list<T>& il) {

}

I have to define the constructor, which I will do on my own. I just want to know what this parameter does. Also, my compiler keeps telling me that std::initializer_list member declaration is not found. I've imported it using #include but the issue is still here.

Jacob Macallan
  • 959
  • 2
  • 8
  • 29

3 Answers3

3

std::initializer_list is a class template that holds a sequence of initializers, which has a very minimal interface since its only purpose is to be used to initialize another object with the sequence of values it contains. Unlike std::vector or std::deque, std::initializer_list is not intended to be used as a general-purpose container.

The function LinkedQueue<T>::LinkedQueue(const std::initializer_list<T>&) is called when someone attempts to list-initialize a LinkedQueue<T>, like this:

LinkedQueue<int> q {1, 2, 3};

The language automatically constructs a std::initializer_list<int> object which contains the values 1, 2, 3, in that order. This object is passed to the constructor. After the constructor returns, the user expects that the newly constructed LinkedQueue<int> object will contain the values 1, 2, 3, in that order.

You need to write the constructor such that this will be true. To do so, you can iterate over the std::initializer_list<T> object using a range-based for loop and add the values in order to the list. Something like this might work, assuming you have a push_back function and a working default constructor:

template<class T>
LinkedQueue<T>::LinkedQueue(const std::initializer_list<T>& il): LinkedQueue() {
    for (const T& val : il) {
        push_back(val);
    }
}

This delegates to the default constructor to set up the class invariants, then inserts the given initializers in order.

Brian Bi
  • 111,498
  • 10
  • 176
  • 312
2

It allows you to initalize your LinkedQueue with {}. as in

 LinkedQueue<char> theQueue = {'a','b','c','d','e','f','g','h' };

as well as other things, detailed here on a page about it and it's uses.

Jonathan
  • 132
  • 8
  • Thank you for sharing! Also, do you know why I have an error in my parameter saying that "initializer_list member not declared" when I've imported it? – Jacob Macallan Jan 22 '16 at 23:14
  • @Xari You probably didn't compile with C++11. – erip Jan 22 '16 at 23:15
  • Hi, I have this in my C++ Settings/GCC C++ Compiler/Miscellaneous flags -> -c -fmessage-length=0 -std=gnu++11 -std=c++11 – Jacob Macallan Jan 22 '16 at 23:19
  • @Xari check your #include 's and make sure you have `#include ` and your compiler should only be using 1 -std flag, -std=c++11 is strict c++ 11 conformance, while -std=gnu++11 is for c++ 11 conformance with the gnu extensions. I'd remove the -std=c++11 for now, and if your gcc is up-to-date it's set for -std=gnu++11 by default – Jonathan Jan 22 '16 at 23:25
  • Yes I do. I'm currently googling the error. I've appended it to my settings as well (and included it using #include – Jacob Macallan Jan 22 '16 at 23:27
1

In C++11 an initializer-list is a way to assign content to an object, when several literals are required.

Example, you could initialize a list like:

std::list<int> myList{1,2,3,4,5};

You may write your class, so it accept such syntax.

As a minimalist example, the following function accept a initialization list:

void func( initializer_list<int> intList)
{
    for (auto i: intList) /*whatever*/;
}

// used as:
func({1,2,3,5});

Initialization are required to be of the same type: you cannot mix values from different types in the initialization-list.

Adrian Maire
  • 14,354
  • 9
  • 45
  • 85