[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

template typename and class

Posted on 2009-07-10
22
Medium Priority
?
274 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 8
  • 3
  • +1
22 Comments
 
LVL 53

Expert Comment

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

Expert Comment

by:Infinity08
ID: 24827788
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 680 total points
ID: 24827794
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 12

Expert Comment

by:Let_Me_Be
ID: 24827798
And yes, there is no semantic difference.
0
 
LVL 3

Author Comment

by:Unimatrix_001
ID: 24827800
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
ID: 24827805
>> 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
ID: 24827865
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 720 total points
ID: 24827872
>> 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
ID: 24827876
>> 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
ID: 24827892
Hm, how strange... Ok thank you. Thanks to you also Let_Me_Be for your input. :)
0
 
LVL 40

Assisted Solution

by:evilrix
evilrix earned 600 total points
ID: 24827910
>> '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
ID: 24827933
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
ID: 24827947
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
ID: 24827978
>>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
ID: 24827985
>>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 680 total points
ID: 24828033
> 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
ID: 24828051
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
ID: 24828068
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
ID: 24828093
Thanks to all. :)

Uni.
0
 
LVL 3

Author Closing Comment

by:Unimatrix_001
ID: 31602295
:)
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 24828096
No problem :)
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
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.

650 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