Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Template within template: why "`>>' should be `> >' within a nested template argument list"

I know that when we are using template inside another template, we should write it like this:

vector<pair<int,int> > s;

and if we write it without the whitespace:

vector<pair<int,int>> s;

we will get an error:

`>>' should be `> >' within a nested template argument list

I see this is understandable, but I just can't help but wondering, in which cases will this be really ambiguous?

like image 940
zw324 Avatar asked Jul 14 '11 14:07

zw324


2 Answers

Sometimes you want it to be >>. Consider

boost::array<int, 1024>>2> x; 

In C++03 this successfully parses and creates an array of size 256.

like image 114
Johannes Schaub - litb Avatar answered Sep 18 '22 21:09

Johannes Schaub - litb


It won't ever be ambiguous. This is proven by the fact that in C++0x you don't have to write a space between closing template >s any more.

The thing is that the compilers would prefer to tokenize the input as context-independently as possible. Since C++ is not a context independent language anyway, adding just this one special case isn't going to make things particularly harder.

like image 27
Armen Tsirunyan Avatar answered Sep 17 '22 21:09

Armen Tsirunyan