template: "typename" or "class"?

I

Ivan Vecerina

: 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
 
B

Bob Hairgrove

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".
 
M

Maxim Yegorushkin

Bob said:
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.
 
B

Bob Hairgrove

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.
 
M

Markus Moll

Hi

Bob said:
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
 
B

Bob Hairgrove

Hi



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>
 
V

Victor Bazarov

Bob said:
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
 
H

Howard

Markus Moll said:
Hi



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"
 

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
473,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top