Templates in C++ are nice, form the basis for generic programming and the STL (part of the standard C++ library), and all the other neat tricks you see when you read the glossy magazines.

Even the syntax is straightforward. (Using the STL,) to declare a vector of ints, you just have to say vector<int> x;. A vector of elements of type T is vector<T>. Simple!

So what happens if you want x to be a vector of vectors of ints? Simple logic would suggest

vector<vector<int>> x;
So much for logic. C++ and C share the same lexer. And that lexer believes with all its might that "the next token is always the longest one." And it has excellent reasons for believing that.

See the problem? In the above code the token >> appears! That is not legal C++ code (and cannot be made legal without breaking every lexical convention in C++). The solution is absolut C++: sully your beautiful idea with an ugly kluge. Just add whitespace.

vector<vector<int> > x;
And that's legal C++ (not to mention recommended practice...)

Log in or registerto write something here or to contact authors.