Solved

template typename and class

Posted on 2009-07-10
22
267 Views
Last Modified: 2012-05-07
Hello.

What is the purpose of a template having the options of being a typename and class, from what I've gathered they're identical in all but name, so why?

Thanks,
Uni
0
Comment
Question by:Unimatrix_001
  • 8
  • 8
  • 3
  • +1
22 Comments
 
LVL 53

Expert Comment

by:Infinity08
Comment Utility
template <typename T> is completely equivalent to template <class T> in every respect.
0
 
LVL 53

Expert Comment

by:Infinity08
Comment Utility
Note that this equivalency only holds for template parameters though. Anywhere else, typename and class have different meanings.
0
 
LVL 12

Assisted Solution

by:Let_Me_Be
Let_Me_Be earned 170 total points
Comment Utility
Well, they are just there. I must say I'm really not sure, which one came first and then was kept for compatibility, but nowadays, its just a way of expressing your intent. If you are expecting a class a parameter, write class, if a generic typename, write typename.
0
 
LVL 12

Expert Comment

by:Let_Me_Be
Comment Utility
And yes, there is no semantic difference.
0
 
LVL 3

Author Comment

by:Unimatrix_001
Comment Utility
Evening Infinity! :)

Yes, I'm aware they're identical in functionality, but what I'm asking is why? What is the purpose of having two 'identifiers' for lack of a better word when both do the same job?

Thanks,
Uni
0
 
LVL 53

Expert Comment

by:Infinity08
Comment Utility
>> which one came first and then was kept for compatibility

If my memory serves me well, 'class' came first, and 'typename' was added later (in the context of template parameters). But I'd have to look that up to be sure.
0
 
LVL 3

Author Comment

by:Unimatrix_001
Comment Utility
What was the purpose though in them adding typename at a later date when it offers nothing more?
0
 
LVL 53

Accepted Solution

by:
Infinity08 earned 180 total points
Comment Utility
>> but what I'm asking is why?

The typename keyword was added, because in some cases, the compiler needed a hand in determining whether something is a type or something else (like a declaration). This is especially needed when dealing with templates (it is required for example when a type name depends on a template parameter, because otherwise the compiler doesn't know that it's dealing with a type).

They then decided to add 'typename' as an alternative to 'class' for template parameters, because it made sense.

There's no more complicated or interesting reason than that :)
0
 
LVL 53

Expert Comment

by:Infinity08
Comment Utility
>> when it offers nothing more?

You might see it as being a bit more readable, because not all types used as a template parameter have to be classes. So, some might consider that 'typename' is more correct than 'class'.
0
 
LVL 3

Author Comment

by:Unimatrix_001
Comment Utility
Hm, how strange... Ok thank you. Thanks to you also Let_Me_Be for your input. :)
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 40

Assisted Solution

by:evilrix
evilrix earned 150 total points
Comment Utility
>> 'class' came first, and 'typename' was added later
Correct.

The typename keyword was added to allow ambiguity issues to be resolved when declaring types from templates but it was extended to also allow it to be used to declare template parameters because the class keyword already has a specific meaning that is unrelated to templates and it was felt typename would be better suited (but class was left in for backward compatibility). The class keyword is also used to declare a template template type, which would be pretty messy if the typename keyword isn't used (e.g. template < template < typename T> class >). But as already stated above, in the specific context of declaring a template parameter type they are semantically identical.
0
 
LVL 53

Expert Comment

by:Infinity08
Comment Utility
I think what Let_Me_Be said in http:#24827794 might be a more interesting answer, as it explains how C++ programmers have grown to use these two keywords in the case of template parameters, rather than why the C++ creators have added support for both.
0
 
LVL 40

Expert Comment

by:evilrix
Comment Utility
Your friendly neighbourhood ZA is always happy to re-open this Q if there is any reason to do so. Just ask.
0
 
LVL 3

Author Comment

by:Unimatrix_001
Comment Utility
>>evilrix:
Hm, it seems to me very strange that something like the CPP standard which appears extremely strict would allow two things to be added that are semantically identical... Seems as though that would increase ambiguity rather than decrease it!

>>rather than why the C++ creators have added support for both.
That's what I was particularly interested in. ;)
0
 
LVL 3

Author Comment

by:Unimatrix_001
Comment Utility
>>Your friendly neighbourhood ZA is always happy to re-open this Q if there is any reason to do so. Just ask.
If Infinity thinks that the answer from Let_Me_Be is more appropriate so be it! Infinity? Your call. ;)

Uni
0
 
LVL 12

Assisted Solution

by:Let_Me_Be
Let_Me_Be earned 170 total points
Comment Utility
> Hm, it seems to me very strange that something like the CPP standard which appears extremely strict would allow two things to be added that are semantically identical... Seems as though that would increase ambiguity rather than decrease it!

Actually not, you have a wrong point of view. C and C++ languages are about extreme stability. Not to break anything at almost any cost. Of course by adding big features, you do actually break something here and there, but if it is something as futile as removing one use of a keyword, you can be sure it will be never done (because removing it has no significance). On the other hand, introducing a new use of a keyword, that won't break anything has a good chance to be added if it is meaningful.
0
 
LVL 3

Author Comment

by:Unimatrix_001
Comment Utility
Hm, very well... :)

Evilrix, would you mind reopening the question as I'd like to split the points - seems I closed the question too early. (:

Cheers,
Uni
0
 
LVL 53

Expert Comment

by:Infinity08
Comment Utility
You have to look at the C++ language as a living thing, that is growing, and evolving. Sometimes, something is added to it, but very rarely, if ever, something is removed.


>> If Infinity thinks that the answer from Let_Me_Be is more appropriate so be it! Infinity? Your call. ;)

That's fine - it's your question ;) I just wanted to make sure that Let_Me_Be's replied didn't go by unnoticed :)
0
 
LVL 3

Author Comment

by:Unimatrix_001
Comment Utility
Thanks to all. :)

Uni.
0
 
LVL 3

Author Closing Comment

by:Unimatrix_001
Comment Utility
:)
0
 
LVL 53

Expert Comment

by:Infinity08
Comment Utility
No problem :)
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now