typename vs class

Discussion in 'C++' started by Jess, Jun 6, 2007.

  1. Jess

    Jess Guest

    Hello,

    I've seen two forms of template declarations

    template<typename T>
    class A{...};

    and

    template<class T>
    class B{...};

    what's the difference between "typename" and "class"? I think both
    declare "T" as a type variable.

    Thanks,
    Jess
     
    Jess, Jun 6, 2007
    #1
    1. Advertising

  2. Jess

    Zeppe Guest

    Jess wrote:
    > Hello,
    >
    > I've seen two forms of template declarations
    >
    > template<typename T>
    > class A{...};
    >
    > and
    >
    > template<class T>
    > class B{...};
    >
    > what's the difference between "typename" and "class"? I think both
    > declare "T" as a type variable.
    >


    No difference at all ;)

    Regards,

    Zeppe
     
    Zeppe, Jun 6, 2007
    #2
    1. Advertising

  3. Jess

    Guest

    On Jun 6, 6:14 pm, Zeppe
    <zep_p@.remove.all.this.long.comment.yahoo.it> wrote:
    > Jess wrote:
    > > Hello,

    >
    > > I've seen two forms of template declarations

    >
    > > template<typename T>
    > > class A{...};

    >
    > > and

    >
    > > template<class T>
    > > class B{...};

    >
    > > what's the difference between "typename" and "class"? I think both
    > > declare "T" as a type variable.

    >
    > No difference at all ;)
    >
    > Regards,
    >
    > Zeppe- Hide quoted text -
    >
    > - Show quoted text -


    there is a difference when u do a typedef class vs typdef typename
    i dont recall the exact diff
     
    , Jun 6, 2007
    #3
  4. Jess wrote:
    > Hello,
    >
    > I've seen two forms of template declarations
    >
    > template<typename T>
    > class A{...};
    >
    > and
    >
    > template<class T>
    > class B{...};
    >
    > what's the difference between "typename" and "class"?


    In this situation: "Class" is shorter and faster to type, while many
    find "typename" to be more expressive about the usage and maybe more
    newbie-friendly.

    Originally, only "class" was a keyword in pre-standard C++, so "class"
    was adopted to templates when they were introduced. At some time, the
    language lawyers saw that "typename" was needed (for other reasons), and
    when first introduced, it was also found suitable in this situation.

    --
    rbh
     
    Robert Bauck Hamar, Jun 6, 2007
    #4
  5. Jess

    Bo Persson Guest

    Jess wrote:
    :: Hello,
    ::
    :: I've seen two forms of template declarations
    ::
    :: template<typename T>
    :: class A{...};
    ::
    :: and
    ::
    :: template<class T>
    :: class B{...};
    ::
    :: what's the difference between "typename" and "class"? I think both
    :: declare "T" as a type variable.
    ::

    Used this way, there is no difference.

    In other places, typename and class have distinct uses.


    Bo Persson
     
    Bo Persson, Jun 6, 2007
    #5
  6. wrote:
    > On Jun 6, 6:14 pm, Zeppe
    > <zep_p@.remove.all.this.long.comment.yahoo.it> wrote:
    >> Jess wrote:
    >>> Hello,
    >>> I've seen two forms of template declarations
    >>> template<typename T>
    >>> class A{...};
    >>> and
    >>> template<class T>
    >>> class B{...};
    >>> what's the difference between "typename" and "class"? I think both
    >>> declare "T" as a type variable.

    >> No difference at all ;)
    >>

    >
    > there is a difference when u do a typedef class vs typedef typename
    > i dont recall the exact diff
    >


    The keyword "typename" is needed to tell the compiler that the following
    is a type, and not something else, and it is used when the compiler
    cannot possibly know. Example:

    template <class T> // or <typename T>
    void f() {
    T::x(y);
    }

    At this point, the exact type of T is unknown, and there is no possible
    way of saying what x is. Is x a static member of some class, or is it a
    type? In this situation, the rules of C++ says that T::x is not a type,
    so if it is, then you have to say so with typename:

    template <class T> // or <typename T>
    void f() {
    typename T::x(y); //T::x is a type
    }

    This also effects what y is. In the first example, y must be some global
    variable, but in the latter, y is declared as a T::x (in a really
    horrible manner).

    --
    rbh
     
    Robert Bauck Hamar, Jun 6, 2007
    #6
    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. Alexander Malkis

    class vs. typename

    Alexander Malkis, Apr 6, 2004, in forum: C++
    Replies:
    7
    Views:
    13,219
    psubramani
    Oct 20, 2012
  2. Replies:
    6
    Views:
    648
    Victor Bazarov
    Oct 20, 2005
  3. Victor Bazarov
    Replies:
    6
    Views:
    326
  4. Kobe
    Replies:
    9
    Views:
    483
    Howard
    Feb 17, 2006
  5. Jakob Bieling

    Q: typename or not typename?

    Jakob Bieling, Mar 14, 2006, in forum: C++
    Replies:
    2
    Views:
    377
    Rolf Magnus
    Mar 14, 2006
Loading...

Share This Page