[antlr-interest] C++ puzzle

Paul Johnson gt54-antlr at cyconix.com
Wed Jun 29 06:40:51 PDT 2005


David Wigg wrote:

> protected:
>      typedef typename _Alloc::template
>          rebind<_Ty>::other _Alty;    // Problem statement

I'm just an amateur, but here's my tuppence (you should try this on 
comp.std.c++ or comp.lang.c++):

this is just, I think,

protected:
  typedef _Alloc::rebind<_Ty>::other _Alty;

where _Alloc is a template. However, there are potentially 2 problems 
with this declaration:

1)  The type of _Alloc depends on a template parameter. The compiler 
can't look up rebind to see if it's a template, so it parses the 
statement as

(_Alloc::rebind) < ...

ie. it thinks you're doing a comparison. You have to explicitly indicate 
that rebind is the name of a template by inserting the prefix 'template' 
(_Alloc::template rebind). This can happen for all operators that can 
qualify a name (::, ->, .).

2) _Alloc is a typename in template _String_val. The typename keyword is 
just used to clarify this. Without typename, _Alloc might be considered 
a static member.

See C++ templates, Vandevoorde & Josuttis, 43 & 132. In many cases you 
can just ignore the typename and template keywords.

HTH

Paul




More information about the antlr-interest mailing list