Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > template: "typename" or "class"?

Reply
Thread Tools

template: "typename" or "class"?

 
 
Kobe
Guest
Posts: n/a
 
      02-17-2006
Is there any difference in:

template <class T>

vs.

template <typename T>

?

Thanks,
K
 
Reply With Quote
 
 
 
 
Ivan Vecerina
Guest
Posts: n/a
 
      02-17-2006
"Kobe" <> wrote in message
news6iJf.181514$...
: Is there any difference in:
:
: template <class T>
:
: vs.
:
: template <typename T>
:
: ?

'class' was used historically, before the new
keyword 'typename' was introduced.
Both are now equivalent, so it is a matter of
style choice (unless you need to support a
very old compiler).

My recommendation: use 'typename' by default.
If the parameter needs to be a UDT that supports
a certain set of member functions, then you may
use the 'class' keyword instead, as a hint.

Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Brainbench MVP for C++ <> http://www.brainbench.com


 
Reply With Quote
 
 
 
 
Daniel T.
Guest
Posts: n/a
 
      02-17-2006
In article <x6iJf.181514$>,
Kobe <> wrote:

> Is there any difference in:
>
> template <class T>
>
> vs.
>
> template <typename T>
>
> ?


Not to the compiler.


--
Magic depends on tradition and belief. It does not welcome observation,
nor does it profit by experiment. On the other hand, science is based
on experience; it is open to correction by observation and experiment.
 
Reply With Quote
 
Bob Hairgrove
Guest
Posts: n/a
 
      02-17-2006
On Fri, 17 Feb 2006 11:06:05 GMT, Kobe <> wrote:

>Is there any difference in:
>
>template <class T>
>
>vs.
>
>template <typename T>
>
>?
>
>Thanks,
>K


There is no difference except for the fact that using "class T" in the
above declaration might be somewhat misleading, since any type,
including non-class types such as int, can be used to instantiate the
template.

There are certain corner cases when writing template template
parameters where the keyword "class" must be used. Josuttis and
Vandevoorde give such an example on page 51 of their book "C++
Templates, The Complete Guide".

--
Bob Hairgrove

 
Reply With Quote
 
Maxim Yegorushkin
Guest
Posts: n/a
 
      02-17-2006

Bob Hairgrove wrote:
> On Fri, 17 Feb 2006 11:06:05 GMT, Kobe <> wrote:
>
> >Is there any difference in:
> >
> >template <class T>
> >
> >vs.
> >
> >template <typename T>
> >
> >?
> >
> >Thanks,
> >K

>
> There is no difference except for the fact that using "class T" in the
> above declaration might be somewhat misleading, since any type,
> including non-class types such as int, can be used to instantiate the
> template.


Well, I've seen this advice too many times and strongly believe it's a
wrong one. Wrong because a compiler won't enforce the advocated policy
of accepting only class types for "class" placeholders. Also it
suggests people dedicate some time to decide whether to put class or
typename keyword in angle brackets. It's trying to find a reason where
there is none.

My advice is always use class keyword in angle brackets simply because
it is shorter to type. Use static assert / enable_if to enforce a
policy on template arguments and to clearly communicate your intent to
the reader of the code. This eliminates waste of time and energy
resulting from deciding which keyword to use and from trying to
understand what meaning that keyword was intended to convey by the
author of the code.

 
Reply With Quote
 
Bob Hairgrove
Guest
Posts: n/a
 
      02-17-2006
On 17 Feb 2006 06:17:36 -0800, "Maxim Yegorushkin"
<> wrote:

>My advice is always use class keyword in angle brackets simply because
>it is shorter to type.


Who types any more? With the IDE I use, I hit "ty" and then
Ctrl-Space, then I can choose "typename" out of the list of C++
keywords which pops up.

My comment was not actually intended as advice, but as a possible
reason in answer of the question why one form might be preferred by
some people over another. Both are fine with me.

--
Bob Hairgrove

 
Reply With Quote
 
Markus Moll
Guest
Posts: n/a
 
      02-17-2006
Hi

Bob Hairgrove wrote:

> Who types any more? With the IDE I use, I hit "ty" and then
> Ctrl-Space, then I can choose "typename" out of the list of C++
> keywords which pops up.


Tsts... an editor. How old-fashioned.
I'm programming by drawing UML diagrams, from which the code gets
automatically generated. Very convenient, indeed

SCNR
Markus

 
Reply With Quote
 
Bob Hairgrove
Guest
Posts: n/a
 
      02-17-2006
On Fri, 17 Feb 2006 16:03:42 +0100, Markus Moll
<> wrote:

>Hi
>
>Bob Hairgrove wrote:
>
>> Who types any more? With the IDE I use, I hit "ty" and then
>> Ctrl-Space, then I can choose "typename" out of the list of C++
>> keywords which pops up.

>
>Tsts... an editor. How old-fashioned.
>I'm programming by drawing UML diagrams, from which the code gets
>automatically generated. Very convenient, indeed
>
>SCNR
>Markus


So does the UML-generated code use "typename" or "class"? <g>

--
Bob Hairgrove

 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      02-17-2006
Bob Hairgrove wrote:
> On Fri, 17 Feb 2006 16:03:42 +0100, Markus Moll
> <> wrote:
>
>
>>Hi
>>
>>Bob Hairgrove wrote:
>>
>>
>>>Who types any more? With the IDE I use, I hit "ty" and then
>>>Ctrl-Space, then I can choose "typename" out of the list of C++
>>>keywords which pops up.

>>
>>Tsts... an editor. How old-fashioned.
>>I'm programming by drawing UML diagrams, from which the code gets
>>automatically generated. Very convenient, indeed
>>
>>SCNR
>>Markus

>
>
> So does the UML-generated code use "typename" or "class"? <g>


Ask if they know where on their hard drive to look for the source...
Isn't the whole purpose of UML to eliminate the need to ever care what
goes into the code?

V
--
Please remove capital As from my address when replying by mail
 
Reply With Quote
 
Howard
Guest
Posts: n/a
 
      02-17-2006

"Markus Moll" <> wrote in message
news:newscache$667uui$xp7$...
> Hi
>
> Bob Hairgrove wrote:
>
>> Who types any more? With the IDE I use, I hit "ty" and then
>> Ctrl-Space, then I can choose "typename" out of the list of C++
>> keywords which pops up.

>
> Tsts... an editor. How old-fashioned.
> I'm programming by drawing UML diagrams, from which the code gets
> automatically generated. Very convenient, indeed
>
> SCNR
> Markus
>


Typing? Code completion? Code generation? Barbarians! Zen programming is
the only way!

-Howard

"I am, therefore I am"





 
Reply With Quote
 
 
 
Reply

Thread Tools

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

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




Advertisments
 



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 47 48 49 50 51 52 53 54 55 56 57