typename vs class

Discussion in 'C++' started by EvilOldGit@googlemail.com, Mar 19, 2007.

  1. Guest

    Are these just aliases ?
    A few docs I've read imply that in certain case they aren't identical.
    , Mar 19, 2007
    #1
    1. Advertising

  2. * :
    > Are these just aliases ?


    No.


    > A few docs I've read imply that in certain case they aren't identical.


    In a template parameter list they are identical, in all other cases they
    aren't identical.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
    Alf P. Steinbach, Mar 19, 2007
    #2
    1. Advertising

  3. blangela Guest

    On Mar 19, 11:26 am, "Alf P. Steinbach" <> wrote:
    > * :
    >
    > > Are these just aliases ?

    >
    > No.
    >
    > > A few docs I've read imply that in certain case they aren't identical.

    >
    > In a template parameter list they are identical, in all other cases they
    > aren't identical.
    >
    > --
    > A: Because it messes up the order in which people normally read text.
    > Q: Why is it such a bad thing?
    > A: Top-posting.
    > Q: What is the most annoying thing on usenet and in e-mail?


    Where is typename used other than in a template paramater list?
    blangela, Mar 19, 2007
    #3
  4. Marcus Kwok Guest

    blangela <> wrote:
    > On Mar 19, 11:26 am, "Alf P. Steinbach" <> wrote:
    >> In a template parameter list they are identical, in all other cases they
    >> aren't identical.

    >
    > Where is typename used other than in a template paramater list?


    One example is in disambiguating dependant names, e.g.,

    template <typename T>
    class xyz {
    void foo()
    {
    typename T::x my_x;
    }
    };

    (example modified from
    http://www.comeaucomputing.com/techtalk/templates/#typename
    which is a handy reference)

    --
    Marcus Kwok
    Replace 'invalid' with 'net' to reply
    Marcus Kwok, Mar 19, 2007
    #4
  5. Fei Liu Guest

    blangela wrote:
    > On Mar 19, 11:26 am, "Alf P. Steinbach" <> wrote:
    >> * :
    >>
    >>> Are these just aliases ?

    >> No.
    >>
    >>> A few docs I've read imply that in certain case they aren't identical.

    >> In a template parameter list they are identical, in all other cases they
    >> aren't identical.
    >>
    >> --
    >> A: Because it messes up the order in which people normally read text.
    >> Q: Why is it such a bad thing?
    >> A: Top-posting.
    >> Q: What is the most annoying thing on usenet and in e-mail?

    >
    > Where is typename used other than in a template paramater list?
    >


    Consider typename a more generic form to declare template parameters.
    Use typename whenever you can instead of class.

    Of course when you declare a class data structure, use 'class'. Only
    place I am aware of that typename cannot replace class keyword.

    Fei
    Fei Liu, Mar 19, 2007
    #5
  6. kwikius Guest

    On 19 Mar, 19:19, Fei Liu <> wrote:

    > Of course when you declare a class data structure, use 'class'. Only
    > place I am aware of that typename cannot replace class keyword.



    Other place is template template params e.g:

    // for allocator
    #include <memory>

    // can't replace 'class' with typename below
    template <template<typename, typename> class Seq>
    struct F{

    template <typename T>
    void foo( Seq<T,std::allocator<T> > const & seq)
    {
    std::cout << *seq.begin() <<'\n';
    }
    };

    #include <vector>
    #include <iostream>

    int main()
    {
    std::vector<int> x;
    F<std::vector> f;

    x.push_back(1);

    f.foo(x);

    }

    regards
    Andy Little
    kwikius, Mar 19, 2007
    #6
  7. On 2007-03-19 19:26, Alf P. Steinbach wrote:
    > * :
    >> Are these just aliases ?

    >
    > No.
    >
    >
    >> A few docs I've read imply that in certain case they aren't identical.

    >
    > In a template parameter list they are identical, in all other cases they
    > aren't identical.


    There's also a case where you must use class instead of template, I ran
    into this some time ago:

    template<template<typename T> class U>

    --
    Erik Wikström
    =?ISO-8859-1?Q?Erik_Wikstr=F6m?=, Mar 19, 2007
    #7
  8. On 2007-03-19 21:42, Erik Wikström wrote:
    > On 2007-03-19 19:26, Alf P. Steinbach wrote:
    >> * :
    >>> Are these just aliases ?

    >>
    >> No.
    >>
    >>
    >>> A few docs I've read imply that in certain case they aren't identical.

    >>
    >> In a template parameter list they are identical, in all other cases they
    >> aren't identical.

    >
    > There's also a case where you must use class instead of template, I ran
    > into this some time ago:
    >
    > template<template<typename T> class U>


    I meant 'use class instead of typename'.

    --
    Erik Wikström
    =?ISO-8859-1?Q?Erik_Wikstr=F6m?=, Mar 19, 2007
    #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. Alexander Malkis

    class vs. typename

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

    Q: typename or not typename?

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

Share This Page