Go Back   Velocity Reviews > Newsgroups > C++
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply

C++ - template declaration

 
Thread Tools Search this Thread
Old 01-13-2008, 12:11 PM   #1
Default 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!!!


Rahul
  Reply With Quote
Old 01-13-2008, 12:23 PM   #2
Erik Wikström
 
Posts: n/a
Default Re: template declaration
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


Erik Wikström
  Reply With Quote
Old 01-13-2008, 03:35 PM   #3
Barry
 
Posts: n/a
Default Re: template declaration
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


Barry
  Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
error: ISO C++ forbids declaration of ‘vector’ with no type samsneelam Software 0 08-28-2008 11:20 AM
Looking for 2-up CD label software or template M.L. DVD Video 14 05-31-2007 01:49 AM
C++ help nastykae General Help Related Topics 0 09-20-2006 06:26 AM
Shinto, Normism & Declaration of Heaven on Earth937 Maria Barti A+ Certification 0 03-16-2005 12:54 PM
what do i need to write DVDs? Marcellus Wallace DVD Video 91 12-02-2003 02:55 PM




SEO by vBSEO 3.3.2 ©2009, Crawlability, Inc.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46