Problem with 'template' as a qualifier

Rodolfo Lima rodolfo@rodsoft.org
Wed May 28 09:01:00 GMT 2008


John Fine escreveu:
> In X::Y::Z when X has already been identified as a type (templated, so 
> its internals are unknown), I think Y might still be a namespace, but 
> I'm very unsure (haven't tested).  Anyway, some of the compilers I use 
> insist on being told that Y is a type.

I don't think that Y might be a type, as there's nothing that I know of 
that states that a namespace can be enclosed in a class declaration.
So, if X::Y names a namespace, X must be a namespace, and so on (X::Y::Z 
is a namespace, X::Y also is).


> Anyway, I thought your issue was with template rather than with 
> typename.  I'd prefer if the standard made the compiler deduce template 
> in cases that can't be parsed any other way, rather than make the 
> programmer clutter the code with it.  But for template, at least I 
> understand why deducing it would be hard on the structure of the 
> compiler and I understand when it is needed and what it qualifies.

I think it's good for the compiler to detect template declaration errors 
earlier than only during instantiation, that's why there's all this fuss 
about dependent names and non-dependent names. But it should use the 
context in which the name is used to tell if it's dealing with a type or 
something else. If it can't, well, the user should tell it what s/he 
mean. I don't know how difficult it is for the compiler to handle this 
issue, but with the rise of template metaprogramming, I find myself 
writing 'typename' more and more, which is annoying when it's obvious 
that I want a type.

Now, correcting what I said before, you really should use 'typename' in 
variable declarations, according to Stroustrup (TC++PL, C13.5):

template <class T> void g(T &y)
{
      T::x(y);
}

This may be a function call, calling T::x function with the parameter y, 
or... surprise, declaring a variable 'y' of the type T::x, with 
redundant parentheses around y. Why the parentheses are allowed is 
beyond me. If they weren't allowed in the first place, typename 
disambiguation shouldn't be necessary. So the committee don't allow 
something useful (not needing typename) because of something redundant. 
There's the intellectual purity over usability that you've mentioned.


Regards,
rod



More information about the Gcc-help mailing list