| Topic: |
DEVELOP > c-Plus-Plus |
| User: |
"Kobe" |
| Date: |
17 Feb 2006 05:06:05 AM |
| Object: |
template: "typename" or "class"? |
Is there any difference in:
template <class T>
vs.
template <typename T>
?
Thanks,
K
.
|
|
| User: "Ivan Vecerina" |
|
| Title: Re: template: "typename" or "class"? |
17 Feb 2006 05:16:52 AM |
|
|
"Kobe" <kobe05@email.it> wrote in message
news:x6iJf.181514$65.4985627@twister1.libero.it...
: 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
.
|
|
|
|
| User: "Daniel T." |
|
| Title: Re: template: "typename" or "class"? |
17 Feb 2006 05:50:37 AM |
|
|
In article <x6iJf.181514$65.4985627@twister1.libero.it>,
Kobe <kobe05@email.it> 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.
.
|
|
|
|
| User: "Bob Hairgrove" |
|
| Title: Re: template: "typename" or "class"? |
17 Feb 2006 07:28:35 AM |
|
|
On Fri, 17 Feb 2006 11:06:05 GMT, Kobe <kobe05@email.it> 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
NoSpamPlease@Home.com
.
|
|
|
| User: "Maxim Yegorushkin" |
|
| Title: Re: template: "typename" or "class"? |
17 Feb 2006 08:17:36 AM |
|
|
Bob Hairgrove wrote:
On Fri, 17 Feb 2006 11:06:05 GMT, Kobe <kobe05@email.it> 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.
.
|
|
|
| User: "Bob Hairgrove" |
|
| Title: Re: template: "typename" or "class"? |
17 Feb 2006 08:56:17 AM |
|
|
On 17 Feb 2006 06:17:36 -0800, "Maxim Yegorushkin"
<maxim.yegorushkin@gmail.com> 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
NoSpamPlease@Home.com
.
|
|
|
| User: "Markus Moll" |
|
| Title: Re: template: "typename" or "class"? |
17 Feb 2006 09:03:42 AM |
|
|
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
.
|
|
|
| User: "Howard" |
|
| Title: Re: template: "typename" or "class"? |
17 Feb 2006 10:42:22 AM |
|
|
"Markus Moll" <moll@rbg.informatik.tu-darmstadt.de> wrote in message
news:newscache$667uui$xp7$1@luna.igd.fhg.de...
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"
.
|
|
|
|
| User: "Bob Hairgrove" |
|
| Title: Re: template: "typename" or "class"? |
17 Feb 2006 09:33:59 AM |
|
|
On Fri, 17 Feb 2006 16:03:42 +0100, Markus Moll
<moll@rbg.informatik.tu-darmstadt.de> 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
NoSpamPlease@Home.com
.
|
|
|
| User: "Victor Bazarov" |
|
| Title: Re: template: "typename" or "class"? |
17 Feb 2006 10:16:14 AM |
|
|
Bob Hairgrove wrote:
On Fri, 17 Feb 2006 16:03:42 +0100, Markus Moll
<moll@rbg.informatik.tu-darmstadt.de> 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
.
|
|
|
|
|
|
|
|
|

|
Related Articles |
|
|