class vs. typename

Discussion in 'C++' started by Alexander Malkis, Apr 6, 2004.

  1. 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.
     
    Alexander Malkis, Apr 6, 2004
    #1
    1. Advertising

  2. "Alexander Malkis" <-sb.de> wrote in message
    news:c4uupa$cbc$-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
     
    John Harrison, Apr 6, 2004
    #2
    1. Advertising

  3. John Harrison wrote:

    >
    > "Alexander Malkis" <-sb.de> wrote in
    > message news:c4uupa$cbc$-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
     
    Steven T. Hatton, Apr 6, 2004
    #3
  4. Alexander Malkis

    Leor Zolman Guest

    On Tue, 6 Apr 2004 20:45:09 +0100, "John Harrison"
    <> wrote:

    >
    >"Alexander Malkis" <-sb.de> wrote in message
    >news:c4uupa$cbc$-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
     
    Leor Zolman, Apr 6, 2004
    #4
  5. 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/
     
    Asfand Yar Qazi, Apr 7, 2004
    #5
  6. 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
     
    Steven T. Hatton, Apr 11, 2004
    #6
  7. Alexander Malkis

    NPC Guest

    "Steven T. Hatton" <> wrote in message
    news:...
    > 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
     
    NPC, Apr 12, 2004
    #7
  8. Alexander Malkis

    psubramani

    Joined:
    Oct 20, 2012
    Messages:
    1
    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
     
    psubramani, Oct 20, 2012
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    6
    Views:
    646
    Victor Bazarov
    Oct 20, 2005
  2. Victor Bazarov
    Replies:
    6
    Views:
    324
  3. Kobe
    Replies:
    9
    Views:
    481
    Howard
    Feb 17, 2006
  4. Jakob Bieling

    Q: typename or not typename?

    Jakob Bieling, Mar 14, 2006, in forum: C++
    Replies:
    2
    Views:
    374
    Rolf Magnus
    Mar 14, 2006
  5. typename vs class

    , Mar 19, 2007, in forum: C++
    Replies:
    7
    Views:
    414
    =?ISO-8859-1?Q?Erik_Wikstr=F6m?=
    Mar 19, 2007
Loading...

Share This Page