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
LVL 3
Unimatrix_001Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Infinity08Commented:
template <typename T> is completely equivalent to template <class T> in every respect.
0
Infinity08Commented:
Note that this equivalency only holds for template parameters though. Anywhere else, typename and class have different meanings.
0
Let_Me_BeCommented:
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
Starting with Angular 5

Learn the essential features and functions of the popular JavaScript framework for building mobile, desktop and web applications.

Let_Me_BeCommented:
And yes, there is no semantic difference.
0
Unimatrix_001Author 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
0
Infinity08Commented:
>> 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
Unimatrix_001Author Commented:
What was the purpose though in them adding typename at a later date when it offers nothing more?
0
Infinity08Commented:
>> 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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Infinity08Commented:
>> 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
Unimatrix_001Author Commented:
Hm, how strange... Ok thank you. Thanks to you also Let_Me_Be for your input. :)
0
evilrixSenior Software Engineer (Avast)Commented:
>> '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
Infinity08Commented:
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
evilrixSenior Software Engineer (Avast)Commented:
Your friendly neighbourhood ZA is always happy to re-open this Q if there is any reason to do so. Just ask.
0
Unimatrix_001Author 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. ;)
0
Unimatrix_001Author 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
0
Let_Me_BeCommented:
> 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
Unimatrix_001Author 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
0
Infinity08Commented:
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
Unimatrix_001Author Commented:
Thanks to all. :)

Uni.
0
Unimatrix_001Author Commented:
:)
0
Infinity08Commented:
No problem :)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.