class vs. typename

  • Thread starter Alexander Malkis
  • Start date
A

Alexander Malkis

What's the semantical/syntactical difference between two keywords
"class" and "typename" (apart from different spelling)?
 
J

John Harrison

Alexander Malkis said:
What's the semantical/syntactical difference between two keywords
"class" and "typename" (apart from different spelling)?

None at all.

I think typename was introduced because not all template parameters are
classes.

john
 
S

Steven T. Hatton

John said:
None at all.

I think typename was introduced because not all template parameters are
classes.

john
I believe we need to restrict the discussion to template parameters in order
for that to hold. This is from the C++ Standard:

"There is no semantic difference between class and typename in a
template-parameter."

Just being pedantic.
 
L

Leor Zolman

None at all.

I think typename was introduced because not all template parameters are
classes.
Actually it was introduced in order to inform the compiler that a name
dependent upon a template parameter is a type; Standard C++ assumes that it
is not in that context unless the typename keyword is used. However, many
existing implementations allow the "typename" to be omitted in that
context, some [like gcc] at least warning you about the assumptions being
made.

I think they chose to allow "typename" instead of "class" for template
parameters after the fact, simply as a way to document the nature of the
type parameters expected. This is one of stylistic issues for which the
"pendulum" still seems to be swinging...
-leor
 
A

Asfand Yar Qazi

Alexander said:
What's the semantical/syntactical difference between two keywords
"class" and "typename" (apart from different spelling)?

"class" has less characters in it than "typename", so saves more trees.

:)
 
S

Steven T. Hatton

Alexander said:
What's the semantical/syntactical difference between two keywords
"class" and "typename" (apart from different spelling)?
I don't know if anybody mentioned this, I just learned it. It is discussed
in TC++PL(SE) Appendix C.13.5. Stroustrup explains it as a way of
disambiguating statements in template declarations. For example


template<class C> void h(C& v)
{
typename C::iterator i = v.begin();
}
 
N

NPC

Steven T. Hatton said:
I don't know if anybody mentioned this, I just learned it. It is discussed
in TC++PL(SE) Appendix C.13.5. Stroustrup explains it as a way of
disambiguating statements in template declarations. For example


template<class C> void h(C& v)
{
typename C::iterator i = v.begin();
}

--
STH
Hatton's Law: "There is only One inviolable Law"
KDevelop: http://www.kdevelop.org SuSE: http://www.suse.com
Mozilla: http://www.mozilla.org


Sometimes compilers need some guidance. Typename is a way of telling the
compiler that C::iterator is a C++ Type (as opposed to a C++ function call,
for example).
You will only see typename used in contexts related to programs using
templates.
As far as how/when to use typename : use it when inside of a templated
class/function and refering to a typedef belonging to the templated
parameter. For example, C in you code above is a templated parameter.
iterator is a typedef defined in C. Therefore, you must use the typename
keyword when declaring an object of that type (or if re-typedef'ing (in C):
typedef typename C::iterator IteratorType).

NPC
 
Joined
Oct 20, 2012
Messages
1
Reaction score
0
from the book c++ standard template library
-----------------------------------------------------------------
Keyword typename
The keyword typename was introduced to specify that the identifier that follows is a type. Consider the following example:


template <class T>
Class MyClass {
typename T::SubType * ptr;
...
};

Here, typename is used to clarify that SubType is a type of class T. Thus, ptr is a pointer to the type T::SubType. Without typename, SubType would be considered a static member. Thus


T::SubType * ptr

would be a multiplication of value SubType of type T with ptr.

According to the qualification of SubType being a type, any type that is used in place of T must provide an inner type SubType. For example, the use of type Q as a template argument
 

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

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,280
Latest member
BGBBrock56

Latest Threads

Top