Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

template<typename> vs. template<class>

16 views
Skip to first unread message

jason.c...@gmail.com

unread,
May 23, 2008, 12:08:45 PM5/23/08
to
Is there any difference between declaring a template parameter as a
"typename" or a "class"? E.g.

template <class T> T f() { }
template <typename T> T g() { }

Thanks,
Jason

Rolf Magnus

unread,
May 23, 2008, 12:09:35 PM5/23/08
to
jason.c...@gmail.com wrote:

> Is there any difference between declaring a template parameter as a
> "typename" or a "class"? E.g.

No. They are equivalent.

Juha Nieminen

unread,
May 23, 2008, 1:52:40 PM5/23/08
to
jason.c...@gmail.com wrote:
> Is there any difference between declaring a template parameter as a
> "typename" or a "class"?

Yes: 'class' is more confusing because the type might not actually be
a class at all.

Bo Persson

unread,
May 23, 2008, 2:44:28 PM5/23/08
to

And typename is more confusing because it has other uses as well. :-)

template<typename Container, typename Container::value_type>
class something;

One typename is the same as class, but not the other. Isn't that
confusing?


Bo Persson


Erik Wikström

unread,
May 23, 2008, 2:54:07 PM5/23/08
to

template<class T>
class Foo { };

One class is the same as typename, but not the other. Isn't that
confusing? :-)

Seriously though, there is at least one instance where they are not
identical:

template<template<typename U> class T>
class Foo { };

--
Erik Wikström

red floyd

unread,
May 24, 2008, 2:08:56 AM5/24/08
to

As others have said, in this case "class" and "typename" have identical
semantics.

That said, I tend to use them both in templates as sort of
documentation, and a usage aid.

e.g.:

template<typename T> something... indicates that T can be any type

template<class T> something... indicates that T should be a class type.

red floyd

unread,
May 24, 2008, 2:12:33 AM5/24/08
to

Follow up note, to remove all confusion.

This is merely a *coding convention* that *I use*, it is not mandated by
the language.

Juha Nieminen

unread,
May 24, 2008, 4:07:04 AM5/24/08
to
Erik Wikström wrote:
> template<template<typename U> class T>
> class Foo { };

Btw, is there any tutorial or other resources in the net which would
explain in detail all that can be done with templates?

Usually you can find just the basic usage and that's it.

Ian Collins

unread,
May 24, 2008, 4:09:42 AM5/24/08
to

If you want that level of detain, get hold of a copy of the excellent
"C++ Templates" by Vandevoorde and Josuttis.

--
Ian Collins.

Bo Persson

unread,
May 24, 2008, 7:48:43 AM5/24/08
to

Yes, in some places you must use class, in some places you must use
typename, and in other places you can use either.

This really *is* confusing, and I believe that there are now some
regrets for allowing this. Seemed like a good idea at the time, but...

Bo Persson


ManicQin

unread,
May 24, 2008, 11:03:16 AM5/24/08
to
0 new messages