template<typename> vs. template<class>

J

jason.cipriani

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
 
J

Juha Nieminen

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.
 
B

Bo Persson

Juha said:
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
 
E

Erik Wikström

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 U> class T>
class Foo { };
 
R

red floyd

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() { }

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.
 
R

red floyd

red said:
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.

Follow up note, to remove all confusion.

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

Juha Nieminen

Erik said:
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.
 
I

Ian Collins

Juha said:
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.
 
B

Bo Persson

Erik said:
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 { };

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
 

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,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top