473,549 Members | 2,723 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

template<typena me> 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 3521
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<typena me Container, typename Container::valu e_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<typena me Container, typename Container::valu e_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<templa te<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<typena me 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<typena me 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<templa te<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<templa te<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<typen ame Container, typename Container::valu e_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<templa te<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

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

Similar topics

17
1755
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
2432
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. i would forget overriding it but i have to do it because its a coursework. here is a simple version of the class #include <iostream> #include...
4
1478
by: ben | last post by:
template <typename T> class A { friend class T; // error, but why? };
23
2622
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 ownership expectation on auto_pointer.
7
3466
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 dt { std::deque< class dt > dtdq; };
14
1805
by: SoilMan | last post by:
Consider the following: class xyz { public: template <typename T> void foo(T x) { cout << "foo<T> " << x << endl; }
6
14978
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> template<class TObject>
4
2305
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 Tparent::Tptr Tptr; Consider example:
4
1413
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
7450
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...
0
7957
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
6043
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...
1
5368
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5088
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3500
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...
0
3481
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1059
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
763
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...

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.