C# 2.0: no more lexer blues with nested templates

When generics got announced and I laid my hands on the C#2.0 compiler the first thing I tried is nested template usage. This C++ compiler (spec actually) gave me enough pain with this. Consider the following C++ code which uses the STL.

#include "vector"

using namespace std;

int main()


vector<int> vectorI;


vector<vector<int>> vectorV;


return 0;


First glance indicates that the line in bold is trying to create a vector of vectors. This is where the infamous C++ lexer blue starts. The lexer thinks that the >> is actually the right-shift operator and spits out an error. On some compiler the error is very cryptic and is something like "undefined symbol vectorV" on some its clearer and is "space required between adjacent ">" delimiters of nested template argument lists". The solution in C++ is to use a space between the two > and write it as

vector<vector<int> > vectorV;

Since I always forgot the space in my first attempt, I landed up swearing when I got this error. However many C++ compiler choose to deviate from the C++ spec and actually act intelligently and treat the consecutive >> as nested template delimiters based on context. I tried the above sample on Microsoft C++ compiler 14.00.50727.18 and it worked. So either MS compiler deviates from the C++ spec or maybe C++ spec has changed (I am not following the C++ world for a long time now....)

The fact that this was a major pain in C++ helped in the design of C#. With C# 2.0 the >> and >>= tokens got removed from the lexical grammer (See the §20.10 in the 2.0 spec) and replaced with new assignment productions. So there is no requirement for the extra space in between >> in C#. So a similar code in C# is like

List<int> listI = new List<int>();


List<List<int>> listL = new List<List<int>>(); // no space reqd


Comments (0)

Skip to main content