Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   class vs. typename (http://www.velocityreviews.com/forums/t282114-class-vs-typename.html)

Alexander Malkis 04-06-2004 07:00 PM

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

--
Best regards,
Alex.

PS. To email me, remove "loeschedies" from the email address given.

John Harrison 04-06-2004 07:45 PM

Re: class vs. typename
 

"Alexander Malkis" <alexloeschediesmalk@stone.cs.uni-sb.de> wrote in message
news:c4uupa$cbc$1@hades.rz.uni-saarland.de...
> 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



Steven T. Hatton 04-06-2004 07:59 PM

Re: class vs. typename
 
John Harrison wrote:

>
> "Alexander Malkis" <alexloeschediesmalk@stone.cs.uni-sb.de> wrote in
> message news:c4uupa$cbc$1@hades.rz.uni-saarland.de...
>> 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

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.
--
p->m == (*p).m == p[0].m
http://www.kdevelop.org
http://www.suse.com
http://www.mozilla.org

Leor Zolman 04-06-2004 09:34 PM

Re: class vs. typename
 
On Tue, 6 Apr 2004 20:45:09 +0100, "John Harrison"
<john_andronicus@hotmail.com> wrote:

>
>"Alexander Malkis" <alexloeschediesmalk@stone.cs.uni-sb.de> wrote in message
>news:c4uupa$cbc$1@hades.rz.uni-saarland.de...
>> 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.

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

>
>john
>


--
Leor Zolman --- BD Software --- www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix
C++ users: Download BD Software's free STL Error Message Decryptor at:
www.bdsoft.com/tools/stlfilt.html

Asfand Yar Qazi 04-07-2004 03:24 PM

Re: class vs. typename
 
Alexander Malkis wrote:
> 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.

:-)


--
http://www.it-is-truth.org/

Steven T. Hatton 04-11-2004 08:23 PM

Re: class vs. typename
 
Alexander Malkis wrote:

> 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();
}

--
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

NPC 04-12-2004 04:00 AM

Re: class vs. typename
 

"Steven T. Hatton" <susudata@setidava.kushan.aa> wrote in message
news:MpKdnaADE6a_NOTdRVn-jA@speakeasy.net...
> Alexander Malkis wrote:
>
> > 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();
> }
>
> --
> 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



psubramani 10-20-2012 09:03 PM

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


All times are GMT. The time now is 10:47 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.