Template template parameters

I

IR

Hello,

Does anyone know why this declaration compiles:

template< template<typename> class T>
class X { /*...*/ };

while this one doesn't:

template< template<typename> typename T>
class X { /*...*/ };


I thought "class" and "typename" were equivalent in template
declarations?

I don't think it's my compiler's fault (VC8) because I couldn't find
references to syntaxes like the second one on the web, only like the
first one, so there must be a logical reason...

Even Comeau's FAQ uses the first form, without even mentioning the
second one.

Thanks by advance.
 
V

Victor Bazarov

IR said:
Hello,

Does anyone know why this declaration compiles:

template< template<typename> class T>
class X { /*...*/ };

while this one doesn't:

template< template<typename> typename T>
class X { /*...*/ };


I thought "class" and "typename" were equivalent in template
declarations?

They are, but only if you use them alone. What you have here is
a template template argument and you cannot use 'typename' there,
it has to be 'class'. See 14.1/1 for the syntax.
I don't think it's my compiler's fault (VC8) because I couldn't find
references to syntaxes like the second one on the web, only like the
first one, so there must be a logical reason...

Even Comeau's FAQ uses the first form, without even mentioning the
second one.

Because there is no "second one". The syntax rules require "class"
there.

V
 
I

IR

Victor said:
IR wrote: [...]
I thought "class" and "typename" were equivalent in template
declarations?

They are, but only if you use them alone. What you have here is
a template template argument and you cannot use 'typename' there,
it has to be 'class'. See 14.1/1 for the syntax.
[...]

Thanks for the answer Victor.
From now on I'll try to look _also_ in the standard before posting
stupid questions on a newsgroup... ;-)

Cheers.
 
G

Greg Comeau

Does anyone know why this declaration compiles:

template< template<typename> class T>
class X { /*...*/ };

while this one doesn't:

template< template<typename> typename T>
class X { /*...*/ };


I thought "class" and "typename" were equivalent in template
declarations?

I don't think it's my compiler's fault (VC8) because I couldn't find
references to syntaxes like the second one on the web, only like the
first one, so there must be a logical reason...

Even Comeau's FAQ uses the first form, without even mentioning the
second one.

If I understand you, there is no second one. See revision at:

http://www.comeaucomputing.com/techtalk/#typename
 
B

Bo Persson

IR said:
Hello,

Does anyone know why this declaration compiles:

template< template<typename> class T>
class X { /*...*/ };

while this one doesn't:

template< template<typename> typename T>
class X { /*...*/ };


I thought "class" and "typename" were equivalent in template
declarations?

No, they are not. In some places they are eqivalent, in others they
are not. You found one of those!

The opposite example is

template<class T, typename T::some_type>
stuct X;

Here, the class keyword can be replaces by typename, but the typename
in the second parameter cannot be replaced by class.

So, using typename in place of class can be confusing!


Bo Persson
 
I

IR

Bo said:
IR wrote: [snip]
I thought "class" and "typename" were equivalent in template
declarations?

No, they are not. In some places they are eqivalent, in others
they are not. You found one of those!

The opposite example is

template<class T, typename T::some_type>
stuct X;

Here, the class keyword can be replaces by typename, but the
typename in the second parameter cannot be replaced by class.

Isn't this very example a (non-)dependent name lookup disambiguation?
(or whatever it is called... I guess you get the point anyway :p)
 
G

Greg Comeau

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top