Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the significance of the <: syntax in C? [duplicate]

Tags:

c++

c

gcc

Possible Duplicate:
<: cannot begin a template argument list

Did you know that

int a<:10:>;

is equivalent to

int a[10];

?

I was writing some piece of code, where in I have a global namespace and a restricted namespace, say NS1 for now. I have a class called Module in my global namespace and I import some other libraries in NS1, which have a class called Module too. I was trying to create a std::list of my Module, i.e. ::Module inside a function in NS1 and doing so, I got this compilation error

std::list<::Module*> &myModule;

genllvm.cpp:60:11: error: ‘<::’ cannot begin a template-argument list
./genllvm.cpp:60:11: note: ‘<:’ is an alternate spelling for ‘[’. Insert whitespace between ‘<’ and ‘::’
./genllvm.cpp:60:11: note: (if you use ‘-fpermissive’ G++ 

What is the significance of this "<:" syntax?

like image 219
Chethan Ravindranath Avatar asked Nov 29 '22 03:11

Chethan Ravindranath


1 Answers

Its call alternative tokens. C++ have several of them:

 <%     {
 %>     }
 <:     [
 :>     ]
 %:     #
 %:%:   ##
 and    &&
 bitor  |
 or     ||
 xor    ˆ
 compl  ~
 bitand &
 and_eq &=
 or_eq  |=
 xor_eq ˆ=
 not    !
 not_eq !=

You can seen some of the alternative token consists of letters. So you can write if (a<b and b<c) in a compiler which can correctly handle them. Their existence is for lack of symbols in keyboards or character sets. The alternative tokens are never replaced with the primary one (unlike trigraphs), but them behave the same as the primary one.

However, C++0x require special treatment for <:: (2.5p3):

Otherwise, if the next three characters are <:: and the subsequent character is neither : nor >, the < is treated as a preprocessor token by itself and not as the first character of the alternative token <:.

So that SomeTemplate<::SomeClass> can be correctly handled.

like image 161
fefe Avatar answered Dec 04 '22 13:12

fefe