template declaration



 DEVELOP > c-Plus-Plus > template declaration

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "Rahul"
Date: 13 Jan 2008 06:11:04 AM
Object: template declaration
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!!!
.

User: "=?UTF-8?B?RXJpayBXaWtzdHLDtm0=?="

Title: Re: template declaration 13 Jan 2008 06:23:51 AM
On 2008-01-13 13:11, Rahul wrote:

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.
--
Erik Wikström
.

User: "Barry"

Title: Re: template declaration 13 Jan 2008 09:35:24 AM
Rahul wrote:

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
--
Thanks
Barry
.


  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER