4

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?

Community
  • 1
  • 1
Chethan Ravindranath
  • 2,001
  • 2
  • 16
  • 28
  • 1
    They're called digraphs. There are also trigraphs like `??!` being `|`. – Xeo Dec 01 '11 at 08:06
  • Duplicate: [<: cannot begin a template argument list](http://stackoverflow.com/questions/3952648/cannot-begin-a-template-argument-list) and see also: [Why are there digraphs in C and C++?](http://stackoverflow.com/questions/432443/why-are-there-digraphs-in-c-and-c) – Paul R Dec 01 '11 at 08:31

4 Answers4

11

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.

fefe
  • 3,342
  • 2
  • 23
  • 45
8

It's for charsets that don't have [.

§6.4.6-3 (C99)

In all aspects of the language, the six tokens

<: :> <% %> %: %:%:

behave, respectively, the same as the six tokens

[  ]  {  }  #  ##
Community
  • 1
  • 1
cnicutar
  • 178,505
  • 25
  • 365
  • 392
  • 3
    Or probably more for charsets who lacks them.Back in the days of 7-bit charsets. {}[]\ and a few others were often replaced with locale specific umlauts. To not have to write aä2å when using a Swedish computer you could use the alernate form a<:2:> in C ( a(.2.) in Pascal) which is at least slightly more readable. – Fredrik Dec 01 '11 at 08:12
  • @Fredrik Absolutely awesome info. – cnicutar Dec 01 '11 at 08:24
  • Thanks. I was however slightly wrong. a[2] would have been aÄ2Å, not aä2å. But that's just boring details :-) – Fredrik Dec 01 '11 at 11:41
2

It's a mostly historic thing to deal with terminal and code page limitations.

Read the short Wikipedia article on digraphs and trigraphs.

aib
  • 45,516
  • 10
  • 73
  • 79
1

It's called digraph. It was used for when terminals didn't have some of the characters used by C.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621