473,725 Members | 2,169 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

template: "typename" or "class"?

Is there any difference in:

template <class T>

vs.

template <typename T>

?

Thanks,
K
Feb 17 '06 #1
9 3346
"Kobe" <ko****@email.i t> wrote in message
news:x6******** *************@t wister1.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
Feb 17 '06 #2
In article <x6************ *********@twist er1.libero.it>,
Kobe <ko****@email.i t> 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.
Feb 17 '06 #3
On Fri, 17 Feb 2006 11:06:05 GMT, Kobe <ko****@email.i t> 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
No**********@Ho me.com
Feb 17 '06 #4

Bob Hairgrove wrote:
On Fri, 17 Feb 2006 11:06:05 GMT, Kobe <ko****@email.i t> 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.

Feb 17 '06 #5
On 17 Feb 2006 06:17:36 -0800, "Maxim Yegorushkin"
<ma************ ***@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
No**********@Ho me.com
Feb 17 '06 #6
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

Feb 17 '06 #7
On Fri, 17 Feb 2006 16:03:42 +0100, Markus Moll
<mo**@rbg.infor matik.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
automaticall y generated. Very convenient, indeed ;-)

SCNR
Markus


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

--
Bob Hairgrove
No**********@Ho me.com
Feb 17 '06 #8
Bob Hairgrove wrote:
On Fri, 17 Feb 2006 16:03:42 +0100, Markus Moll
<mo**@rbg.infor matik.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
automatical ly 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
Feb 17 '06 #9

"Markus Moll" <mo**@rbg.infor matik.tu-darmstadt.de> wrote in message
news:ne******** ************@lu na.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"

Feb 17 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1876
by: Gianni Mariani | last post by:
I remember seeing a neat template specialization trick posted here a few months ago that allowed the detection of a type containing a member. After a day searching through google archives I come up zip. Anyhow, after much trial and error I came up with this. This "contains_member" template sets value to 1 if the type D contains a typedef named MEMBER of type TD.
0
1235
by: Gianni Mariani | last post by:
It started out as an ugly hack and it turned out to be very easy to do but I'm concerned about wether I'm not seeing somthing so I'm asking the wise ladies and gents of clc++ as to wether they turn to stone. The problem goes like this. I have a class that uses an intrusive reference count to manage the it's life-time. This class has multiple interfaces e.g. (using Austria C++ type smart pointers)...
4
2403
by: pervinder | last post by:
Hi, I get an error on line #3 while compiling the program segment on aix (xlc - va6) While this warning is not seen with gcc "test.cpp" line 200.18: 1540-0152 (W) A template dependent name that is a type must be qualified with "typename". #1 template<class KeyType,
5
1649
by: Michael Olea | last post by:
Here is a design problem I ran into this am - and I have cleaned the bathroom, scrubbed toilet sink and tub, windexed all glass, mopped the floor, and vacuumed the house - no dice, the problem is still there. Maybe y'all have some ideas? Background ========== The basic idea behind templates, of course, is that much code is independent
1
18979
by: Joel Kullberg | last post by:
Hi! I have a question for u guys! I would like to use the c++ package ITK (www.itk.org) for internal handling och data and functions in a dataset3D class of mine! I also want to use a base class to be able to use all kinds of datasets (2D/3D/4D...) ----------------------------------------------------------- class dataset_base{ public: virtual void printDataInfo()=0;
3
4791
by: Maersa | last post by:
Hi All, Was anybody able to serialize the "Type" class properly using XmlSerializer().... Want to serialize and deserialze the Type in a string form, but can this be done ? myobj.Type = typeof(System.String) as <type>System.String</type> thanks a ton.
4
1426
by: Ed | last post by:
Hi, guys, Here is a simple template class definition: template <typename T = int> class Point { public: T X; T Y; T Z; };
2
2056
by: Hukkky | last post by:
File : NodeList.h //---------------------------------------------------------------------- #ifndef NODELIST_H #define NODELIST_H #include <string> using std::string; template <typename T> class NodeList {
6
2883
by: Luna Moon | last post by:
Here is a quote from the book: C++ Annotations, I don't understand these sentences, could anybody help me? thanks! As a side effect to this implementation it must be stressed that it is not anymore correct to declare iostream objects using standard forward declarations, like: class ostream; // now erroneous
0
8888
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8752
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9401
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9176
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9113
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8097
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4519
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4784
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2157
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.