We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

template typename and class

Unimatrix_001
on
Medium Priority
286 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
Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2009

Commented:
template <typename T> is completely equivalent to template <class T> in every respect.
CERTIFIED EXPERT
Top Expert 2009

Commented:
Note that this equivalency only holds for template parameters though. Anywhere else, typename and class have different meanings.
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
And yes, there is no semantic difference.

Author

Commented:
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
CERTIFIED EXPERT
Top Expert 2009

Commented:
>> 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.

Author

Commented:
What was the purpose though in them adding typename at a later date when it offers nothing more?
CERTIFIED EXPERT
Top Expert 2009
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
CERTIFIED EXPERT
Top Expert 2009

Commented:
>> 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'.

Author

Commented:
Hm, how strange... Ok thank you. Thanks to you also Let_Me_Be for your input. :)
evilrixSenior Software Engineer (Avast)
CERTIFIED EXPERT
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
CERTIFIED EXPERT
Top Expert 2009

Commented:
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.
evilrixSenior Software Engineer (Avast)
CERTIFIED EXPERT

Commented:
Your friendly neighbourhood ZA is always happy to re-open this Q if there is any reason to do so. Just ask.

Author

Commented:
>>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. ;)

Author

Commented:
>>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
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
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
CERTIFIED EXPERT
Top Expert 2009

Commented:
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 :)

Author

Commented:
Thanks to all. :)

Uni.

Author

Commented:
:)
CERTIFIED EXPERT
Top Expert 2009

Commented:
No problem :)
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.