typename Vs class: redundant?

R

Rui Maciel

It appears that all tutorials I've read on templates state that the use of the keywords typename and class to specify the template parameter, stating that both expressions have exactly the same meaning.

That seems to be a bit strange. Is that true? If so, what is the purpose of adding perfectly redundant keywords to a language?


Rui Maciel
 
A

Ambar Shukla

It appears that all tutorials I've read on templates state that the use of the keywords typename and class to specify the template parameter, stating that both expressions have exactly the same meaning.

That seems to be a bit strange. Is that true? If so, what is the purpose of adding perfectly redundant keywords to a language?

Rui Maciel

Just want to point out this web-page I found explaining your question.
There are indeed some cases where the keyword class cannot be used and
so typename is required.
http://pages.cs.wisc.edu/~driscoll/typename.html

Hope that helps
Ambar Shukla
 
A

Andrey Tarasevich

Rui said:
It appears that all tutorials I've read on templates state that the use of the keywords typename and class to specify the template parameter, stating that both expressions have exactly the same meaning.

That seems to be a bit strange. Is that true?

When it comes to declaring template parameters, the keyword 'typename'
does indeed have exactly the same meaning as keyword 'class'. There's no
difference whatsoever.
If so, what is the purpose of adding perfectly redundant keywords to a language?

Well, there's really no "adding" here. The keyword 'typename' has other
meaning in C++, when it is used in different context - it is used to
qualify dependent nested type names in declarations. The keyword
'class', as you no doubt know, also has a different meaning when used in
different context - it is used to declare class types. So, basically,
you can think of it that way: instead of introducing a new keyword for
template parameter declaration, the language borrowed an existing one -
'class', - and while it was at it, it decided that borrowing one more -
'typename' - will do no harm. So it did.

Even though they mean the same thing, you are free to develop your own
usage standard governing the selection of a specific keyword in each
specific case (like "use 'class' when a class type is expected, use
'typename' in all other cases"), which might improve the readability of
the code. Or not.
 
A

alfps

It appears that all tutorials I've read on templates state that the use of the keywords typename and class to specify the template parameter, stating that both expressions have exactly the same meaning.

That seems to be a bit strange. Is that true?

No.

In most simple cases it's true.

However, when you specify a template parameter that itself is a
template, you have to use the keyword 'class', like

template< template <typename X> class Policy >
class Foo {};

template< typename T > class Bar {};

int main()
{
Foo<Bar> x;
}

If so, what is the purpose of adding perfectly redundant keywords to a
language?

It isn't so, but: syntactic sugaring, backwards compatibility,
whatever...


Cheers & hth.,

- Alf
 
J

James Kanze

It appears that all tutorials I've read on templates state
that the use of the keywords typename and class to specify the
template parameter, stating that both expressions have exactly
the same meaning.
That seems to be a bit strange. Is that true? If so, what is
the purpose of adding perfectly redundant keywords to a
language?

History. When templates were first introduced, there was no
keyword typename, so class was adopted, to avoid introducing a
new keyword, even though it really can mean any type, and not
just a class here. Later, typename was introduced to solve
other problems (related to two phase lookup); at that point, the
people developing the standard realized that typename really was
a better choice than class for the template parameter, so
adopted it there as well. Without dropping class, since that
would mean breaking a lot of existing code.
 
R

red floyd

James said:
History. When templates were first introduced, there was no
keyword typename, so class was adopted, to avoid introducing a
new keyword, even though it really can mean any type, and not
just a class here. Later, typename was introduced to solve
other problems (related to two phase lookup); at that point, the
people developing the standard realized that typename really was
a better choice than class for the template parameter, so
adopted it there as well. Without dropping class, since that
would mean breaking a lot of existing code.

What I do is to use "typename" when any type can do as a parameter,
and "class" when only a class should be used. Granted, the restriction
isn't enforced by the language, but it's a nice, easy way to have the
code self-document.
 
R

Rolf Magnus

red said:
What I do is to use "typename" when any type can do as a parameter,
and "class" when only a class should be used. Granted, the restriction
isn't enforced by the language, but it's a nice, easy way to have the
code self-document.

I don't really see a point in that. After all, if only a class can be used
as a template parameter, that means that this class needs to support a very
specific interface, which must be explicitly documented anyway.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top