Link to home
Start Free TrialLog in
Avatar of Unimatrix_001
Unimatrix_001Flag for United Kingdom of Great Britain and Northern Ireland

asked on

template typename and class

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
Avatar of Infinity08
Infinity08
Flag of Belgium image

template <typename T> is completely equivalent to template <class T> in every respect.
Note that this equivalency only holds for template parameters though. Anywhere else, typename and class have different meanings.
SOLUTION
Avatar of Let_Me_Be
Let_Me_Be
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
And yes, there is no semantic difference.
Avatar of Unimatrix_001

ASKER

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
>> 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.
What was the purpose though in them adding typename at a later date when it offers nothing more?
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>> 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'.
Hm, how strange... Ok thank you. Thanks to you also Let_Me_Be for your input. :)
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
Your friendly neighbourhood ZA is always happy to re-open this Q if there is any reason to do so. Just ask.
>>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. ;)
>>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
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
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 :)
Thanks to all. :)

Uni.
:)
No problem :)