7

I read an implementation of a Queue from some algorithm books, there is a snippet that is new/interesting to me which I didn't understand. I think it is something new like initialization list for vector in C++11, but I am not that confident due to the context of the code. Can anyone shed a light or provide some reference?

template <typename T>
class Queue {
    private:
        size_t head, tail, count;
        vector<T> data;
    public:
        Queue(const size_t &cap=8) : head(0),tail(0),count(0),data({cap}) {}
        //... more interfaces
        //...
}

the questionable part is data({cap}), what is this? It resizes the vector to a capacity of cap? (Obviously the author of the code intends to give data a size of cap when constructing it.)

EDIT: after read the first answer and test, we know that the book had error in the snippet. It intends to give a initial cap, but it used erroneous {}.

Peiti Li
  • 4,634
  • 9
  • 40
  • 57
  • http://en.cppreference.com/w/cpp/utility/initializer_list – BoBTFish Apr 08 '13 at 15:51
  • 5
    [C++11 Fun with initializer lists, arrays, and enumerations](http://stackoverflow.com/questions/8606315/c11-fun-with-initializer-lists-arrays-and-enumerations). You should probably consult a C++ reference before asking questions. Basic syntax questions can be answered that way. – Raymond Chen Apr 08 '13 at 15:52
  • `{cap}` should be an empty list of size 8 if i'm not mistaken; so it'll initialize it with a default length of 8.... or a list with element 8, so you'll end up with that. – Shark Apr 08 '13 at 15:55
  • @RaymondChen Realistically, most references you're likely to have at hand don't treat C++11. The standard reference is _The C++ Programming Language_, by Bjarne Stroustrup. The currently available edition dates from 1997, and covers C++98. (A new addition is scheduled for release, but you can't get it yet.) – James Kanze Apr 08 '13 at 16:15
  • 1
    @JamesKanze Naturally, if you're asking a question about C++11, you should consult a C++11 reference. – Raymond Chen Apr 08 '13 at 16:41
  • 1
    @James: *Reference* doesn't have to mean dead trees in a binding. – Ben Voigt Apr 08 '13 at 17:52
  • @RaymondChen Are there any good C++11 references available yet. (I use the standard itself, but it's not what I'd recommend for most programmers.) – James Kanze Apr 09 '13 at 09:44
  • @BenVoigt Yes and no. When used as a reference, a hard copy book can be significantly superior to on-line data. More importantly, you know what you're getting; the on line "reference" sites I've seen have been pretty bad. – James Kanze Apr 09 '13 at 09:46
  • @JamesKanze The online references may not be ideal, but they are good enough to answer questions like "What the heck is this wacky new syntax?" You can use the standard itself to get the gory details if that's what you're after. (Though that's not what the OP was after; the OP was just interested in "What the heck is this>") – Raymond Chen Apr 09 '13 at 11:15
  • @James: That's so ridiculous. Plenty of published books of the same low quality as a random "reference" on the internet, and plenty of high-quality material on the Internet that meets or exceeds any published-on-paper book. Yes, you have to find a reliable source. But that's the same problem both online and in print. – Ben Voigt Apr 09 '13 at 13:45
  • @BenVoigt "Plenty of published books of the same low quality": that's for sure. But the books usually cited (and reviewed by the ACCU) are safe bets, where as the links I've seen to date have been pretty bad. – James Kanze Apr 09 '13 at 14:16
  • @BenVoigt (I should add that that's for C++. For other subjects, like Subversion or vim, the links on the Web _are_ the books.) – James Kanze Apr 09 '13 at 14:17
  • It is to imagine how anyone thought the linked "duplicate" was the same as this question. – N. Virgo Jul 05 '14 at 04:03

1 Answers1

4

That is uniform initialization, a new C++11 feature. However, it is arguably being used the right way in your example. It should be:

Queue(const size_t &cap=8) : head(0),tail(0),count(0),data(cap) {}
//                                                        ^^^^^

Because the intention is to invoke the constructor of std::vector<> that accepts the initial size of the vector. Invoking it this way:

data{cap}

Or this way:

data({cap})

Causes the constructor accepting an std::initializer_list to be picked (initializer lists are another new feature of C++11, tightly related to brace initialization), resulting in a vector initialized with one single element whose value is cap.

You can verify the above claim in this live example (code is reported below):

#include <vector>

struct X
{
    X(int s) : v1({s}), v2{s}, v3(s) { }
    std::vector<int> v1;
    std::vector<int> v2;
    std::vector<int> v3;
};

#include <iostream>

int main()
{
    X x(42);
    std::cout << x.v1.size() << std::endl; // Prints 1
    std::cout << x.v2.size() << std::endl; // Prints 1
    std::cout << x.v3.size() << std::endl; // Prints 42
}
mpromonet
  • 11,326
  • 43
  • 62
  • 91
Andy Prowl
  • 124,023
  • 23
  • 387
  • 451