By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,682 Members | 1,096 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,682 IT Pros & Developers. It's quick & easy.

template<typename> vs. template<class>

P: n/a
Is there any difference between declaring a template parameter as a
"typename" or a "class"? E.g.

template <class TT f() { }
template <typename TT g() { }

Thanks,
Jason
Jun 27 '08 #1
Share this Question
Share on Google+
10 Replies


P: n/a
ja************@gmail.com wrote:
Is there any difference between declaring a template parameter as a
"typename" or a "class"? E.g.
No. They are equivalent.

Jun 27 '08 #2

P: n/a
ja************@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.
Jun 27 '08 #3

P: n/a
Juha Nieminen wrote:
ja************@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.
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
Jun 27 '08 #4

P: n/a
On 2008-05-23 20:44, Bo Persson wrote:
Juha Nieminen wrote:
>ja************@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.

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?
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 Uclass T>
class Foo { };

--
Erik Wikström
Jun 27 '08 #5

P: n/a
ja************@gmail.com wrote:
Is there any difference between declaring a template parameter as a
"typename" or a "class"? E.g.

template <class TT f() { }
template <typename TT g() { }
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 Tsomething... indicates that T can be any type

template<class Tsomething... indicates that T should be a class type.
Jun 27 '08 #6

P: n/a
red floyd wrote:
ja************@gmail.com wrote:
>Is there any difference between declaring a template parameter as a
"typename" or a "class"? E.g.

template <class TT f() { }
template <typename TT g() { }

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 Tsomething... indicates that T can be any type

template<class Tsomething... indicates that T should be a class type.
Follow up note, to remove all confusion.

This is merely a *coding convention* that *I use*, it is not mandated by
the language.
Jun 27 '08 #7

P: n/a
Erik Wikström wrote:
template<template<typename Uclass 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.
Jun 27 '08 #8

P: n/a
Juha Nieminen wrote:
Erik Wikström wrote:
> template<template<typename Uclass 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.
If you want that level of detain, get hold of a copy of the excellent
"C++ Templates" by Vandevoorde and Josuttis.

--
Ian Collins.
Jun 27 '08 #9

P: n/a
Erik Wikstrm wrote:
On 2008-05-23 20:44, Bo Persson wrote:
>Juha Nieminen wrote:
>>ja************@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.

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?

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 Uclass T>
class Foo { };
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
Jun 27 '08 #10

P: n/a
On May 24, 11:07*am, Juha Nieminen <nos...@thanks.invalidwrote:
Erik Wikstrm wrote:
* template<template<typename Uclass 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.
http://www.linuxtopia.org/online_boo...mming_104.html
Have fun it's more than you'll ever need
Jun 27 '08 #11

This discussion thread is closed

Replies have been disabled for this discussion.