template declaration

R

Rahul

Hi Everyone,

we use the following in the template declaration,

template <class T>

template<typename T>

Is it that typename is preferred as it can be used for all types,
where as class can only be used for custom class types?

Thanks in advance!!!
 
E

Erik Wikström

Hi Everyone,

we use the following in the template declaration,

template <class T>

template<typename T>

Is it that typename is preferred as it can be used for all types,
where as class can only be used for custom class types?

No, whether you use class or typename here does not matter as far as the
compiler is concerned. I prefer to use typename and only use class for
class declarations.
 
B

Barry

Rahul said:
Hi Everyone,

we use the following in the template declaration,

template <class T>

template<typename T>

Is it that typename is preferred as it can be used for all types,
where as class can only be used for custom class types?

Thanks in advance!!!

Both keywords have the same effect here, so it's just a coding style issue.
IIRC, according to "C++ Template: The complete Guide":

when the template parameter is not always a "class type"(including
/class/ /struct/, /union/), in this case, use /typename/:

e.g.

template <typename T>
class A { T t; };

class B {};

A<B> a1;
A<int> a2; // int is not a class type

In the case when the template parameter should be a "class type", use
/class/,

Additionally, when the template argument is of template template
argument, only /class/ can be used.

e.g.

template <template <typename> class TT>
^^^^^
class A;

HTH
 

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

Forum statistics

Threads
474,184
Messages
2,570,973
Members
47,527
Latest member
RoxanneTos

Latest Threads

Top