473,513 Members | 4,753 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

template<typename> vs. template<class>

Is there any difference between declaring a template parameter as a
"typename" or a "class"? E.g.

template <class TT f() { }
template <typename TT g() { }

Thanks,
Jason
Jun 27 '08 #1
10 3513
ja************@gmail.com wrote:
Is there any difference between declaring a template parameter as a
"typename" or a "class"? E.g.
No. They are equivalent.

Jun 27 '08 #2
ja************@gmail.com wrote:
Is there any difference between declaring a template parameter as a
"typename" or a "class"?
Yes: 'class' is more confusing because the type might not actually be
a class at all.
Jun 27 '08 #3
Juha Nieminen wrote:
ja************@gmail.com wrote:
>Is there any difference between declaring a template parameter as a
"typename" or a "class"?

Yes: 'class' is more confusing because the type might not actually
be a class at all.
And typename is more confusing because it has other uses as well. :-)

template<typename Container, typename Container::value_type>
class something;

One typename is the same as class, but not the other. Isn't that
confusing?
Bo Persson
Jun 27 '08 #4
On 2008-05-23 20:44, Bo Persson wrote:
Juha Nieminen wrote:
>ja************@gmail.com wrote:
>>Is there any difference between declaring a template parameter as a
"typename" or a "class"?

Yes: 'class' is more confusing because the type might not actually
be a class at all.

And typename is more confusing because it has other uses as well. :-)

template<typename Container, typename Container::value_type>
class something;

One typename is the same as class, but not the other. Isn't that
confusing?
template<class T>
class Foo { };

One class is the same as typename, but not the other. Isn't that
confusing? :-)

Seriously though, there is at least one instance where they are not
identical:

template<template<typename Uclass T>
class Foo { };

--
Erik Wikström
Jun 27 '08 #5
ja************@gmail.com wrote:
Is there any difference between declaring a template parameter as a
"typename" or a "class"? E.g.

template <class TT f() { }
template <typename TT g() { }
As others have said, in this case "class" and "typename" have identical
semantics.

That said, I tend to use them both in templates as sort of
documentation, and a usage aid.

e.g.:

template<typename Tsomething... indicates that T can be any type

template<class Tsomething... indicates that T should be a class type.
Jun 27 '08 #6
red floyd wrote:
ja************@gmail.com wrote:
>Is there any difference between declaring a template parameter as a
"typename" or a "class"? E.g.

template <class TT f() { }
template <typename TT g() { }

As others have said, in this case "class" and "typename" have identical
semantics.

That said, I tend to use them both in templates as sort of
documentation, and a usage aid.

e.g.:

template<typename Tsomething... indicates that T can be any type

template<class Tsomething... indicates that T should be a class type.
Follow up note, to remove all confusion.

This is merely a *coding convention* that *I use*, it is not mandated by
the language.
Jun 27 '08 #7
Erik Wikström wrote:
template<template<typename Uclass T>
class Foo { };
Btw, is there any tutorial or other resources in the net which would
explain in detail all that can be done with templates?

Usually you can find just the basic usage and that's it.
Jun 27 '08 #8
Juha Nieminen wrote:
Erik Wikström wrote:
> template<template<typename Uclass T>
class Foo { };

Btw, is there any tutorial or other resources in the net which would
explain in detail all that can be done with templates?

Usually you can find just the basic usage and that's it.
If you want that level of detain, get hold of a copy of the excellent
"C++ Templates" by Vandevoorde and Josuttis.

--
Ian Collins.
Jun 27 '08 #9
Erik Wikström wrote:
On 2008-05-23 20:44, Bo Persson wrote:
>Juha Nieminen wrote:
>>ja************@gmail.com wrote:
Is there any difference between declaring a template parameter
as a "typename" or a "class"?

Yes: 'class' is more confusing because the type might not
actually be a class at all.

And typename is more confusing because it has other uses as well.
:-)

template<typename Container, typename Container::value_type>
class something;

One typename is the same as class, but not the other. Isn't that
confusing?

template<class T>
class Foo { };

One class is the same as typename, but not the other. Isn't that
confusing? :-)

Seriously though, there is at least one instance where they are not
identical:

template<template<typename Uclass T>
class Foo { };
Yes, in some places you must use class, in some places you must use
typename, and in other places you can use either.

This really *is* confusing, and I believe that there are now some
regrets for allowing this. Seemed like a good idea at the time, but...

Bo Persson
Jun 27 '08 #10
On May 24, 11:07*am, Juha Nieminen <nos...@thanks.invalidwrote:
Erik Wikström wrote:
* template<template<typename Uclass T>
* class Foo { };

* Btw, is there any tutorial or other resources in the net which would
explain in detail all that can be done with templates?

* Usually you can find just the basic usage and that's it.
http://www.linuxtopia.org/online_boo...mming_104.html
Have fun it's more than you'll ever need
Jun 27 '08 #11

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

Similar topics

17
1753
by: pub | last post by:
When creating a list: list<class A*> l; How to delete all the objects whose pointers are contained in "l"? Thanks for your comments?
2
2427
by: franklini | last post by:
hello people i. can anybody help me, i dont know what is wrong with this class. it has something to do with the me trying to override the input output stream. if i dont override it, it works fine....
4
1475
by: ben | last post by:
template <typename T> class A { friend class T; // error, but why? };
23
2618
by: guru.slt | last post by:
Hi, see this code: auto_ptr<int> int_auto_p(new int(3)); auto_ptr<int> int_auto_p2(int_auto_p.get()); Then, both auto_pointer will own the same object. That will violate the single...
7
3461
by: Dan Trowbridge | last post by:
He everyone, I am just getting started with .NET and I am having a porting problem. I get and error in code that lookssomething like this (really stripped down but you get the idea)... class...
14
1796
by: SoilMan | last post by:
Consider the following: class xyz { public: template <typename T> void foo(T x) { cout << "foo<T> " << x << endl; }
6
14970
by: Arne Schmitz | last post by:
Why do I get the following error: error: type 'std::vector<TObject*, std::allocator<TObject*' is not derived from type 'bar<TObject>' with the following code: #include <vector> ...
4
2300
by: Grizlyk | last post by:
Hello. Why were base class "typedefs" hidden by template<and explicit usage of them does not work too? Try open only one of the lines in the example below //using Tparent::Tptr; //typedef...
4
1409
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; };
0
7254
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,...
0
7432
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7094
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...
0
7519
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...
0
3230
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...
0
3218
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1585
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
796
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
452
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...

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.