typename Vs class: redundant?

Discussion in 'C++' started by Rui Maciel, Dec 24, 2008.

  1. Rui Maciel

    Rui Maciel Guest

    It appears that all tutorials I've read on templates state that the use of the keywords typename and class to specify the template parameter, stating that both expressions have exactly the same meaning.

    That seems to be a bit strange. Is that true? If so, what is the purpose of adding perfectly redundant keywords to a language?


    Rui Maciel
     
    Rui Maciel, Dec 24, 2008
    #1
    1. Advertising

  2. Rui Maciel

    Ambar Shukla Guest

    On Dec 24, 6:24 pm, Rui Maciel <> wrote:
    > It appears that all tutorials I've read on templates state that the use of the keywords typename and class to specify the template parameter, stating that both expressions have exactly the same meaning.
    >
    > That seems to be a bit strange. Is that true? If so, what is the purpose of adding perfectly redundant keywords to a language?
    >
    > Rui Maciel


    Just want to point out this web-page I found explaining your question.
    There are indeed some cases where the keyword class cannot be used and
    so typename is required.
    http://pages.cs.wisc.edu/~driscoll/typename.html

    Hope that helps
    Ambar Shukla
     
    Ambar Shukla, Dec 24, 2008
    #2
    1. Advertising

  3. Rui Maciel wrote:
    > It appears that all tutorials I've read on templates state that the use of the keywords typename and class to specify the template parameter, stating that both expressions have exactly the same meaning.
    >
    > That seems to be a bit strange. Is that true?


    When it comes to declaring template parameters, the keyword 'typename'
    does indeed have exactly the same meaning as keyword 'class'. There's no
    difference whatsoever.

    > If so, what is the purpose of adding perfectly redundant keywords to a language?


    Well, there's really no "adding" here. The keyword 'typename' has other
    meaning in C++, when it is used in different context - it is used to
    qualify dependent nested type names in declarations. The keyword
    'class', as you no doubt know, also has a different meaning when used in
    different context - it is used to declare class types. So, basically,
    you can think of it that way: instead of introducing a new keyword for
    template parameter declaration, the language borrowed an existing one -
    'class', - and while it was at it, it decided that borrowing one more -
    'typename' - will do no harm. So it did.

    Even though they mean the same thing, you are free to develop your own
    usage standard governing the selection of a specific keyword in each
    specific case (like "use 'class' when a class type is expected, use
    'typename' in all other cases"), which might improve the readability of
    the code. Or not.

    --
    Best regards,
    Andrey Tarasevich
     
    Andrey Tarasevich, Dec 24, 2008
    #3
  4. Rui Maciel

    alfps Guest

    On 24 Des, 19:24, Rui Maciel <> wrote:
    > It appears that all tutorials I've read on templates state that the use of the keywords typename and class to specify the template parameter, stating that both expressions have exactly the same meaning.
    >
    > That seems to be a bit strange. Is that true?


    No.

    In most simple cases it's true.

    However, when you specify a template parameter that itself is a
    template, you have to use the keyword 'class', like

    template< template <typename X> class Policy >
    class Foo {};

    template< typename T > class Bar {};

    int main()
    {
    Foo<Bar> x;
    }


    > If so, what is the purpose of adding perfectly redundant keywords to a
    > language?


    It isn't so, but: syntactic sugaring, backwards compatibility,
    whatever...


    Cheers & hth.,

    - Alf
     
    alfps, Dec 24, 2008
    #4
  5. Rui Maciel

    James Kanze Guest

    On Dec 24, 7:24 pm, Rui Maciel <> wrote:
    > It appears that all tutorials I've read on templates state
    > that the use of the keywords typename and class to specify the
    > template parameter, stating that both expressions have exactly
    > the same meaning.


    > That seems to be a bit strange. Is that true? If so, what is
    > the purpose of adding perfectly redundant keywords to a
    > language?


    History. When templates were first introduced, there was no
    keyword typename, so class was adopted, to avoid introducing a
    new keyword, even though it really can mean any type, and not
    just a class here. Later, typename was introduced to solve
    other problems (related to two phase lookup); at that point, the
    people developing the standard realized that typename really was
    a better choice than class for the template parameter, so
    adopted it there as well. Without dropping class, since that
    would mean breaking a lot of existing code.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, Dec 25, 2008
    #5
  6. Rui Maciel

    red floyd Guest

    James Kanze wrote:
    > On Dec 24, 7:24 pm, Rui Maciel <> wrote:
    >> It appears that all tutorials I've read on templates state
    >> that the use of the keywords typename and class to specify the
    >> template parameter, stating that both expressions have exactly
    >> the same meaning.

    >
    >> That seems to be a bit strange. Is that true? If so, what is
    >> the purpose of adding perfectly redundant keywords to a
    >> language?

    >
    > History. When templates were first introduced, there was no
    > keyword typename, so class was adopted, to avoid introducing a
    > new keyword, even though it really can mean any type, and not
    > just a class here. Later, typename was introduced to solve
    > other problems (related to two phase lookup); at that point, the
    > people developing the standard realized that typename really was
    > a better choice than class for the template parameter, so
    > adopted it there as well. Without dropping class, since that
    > would mean breaking a lot of existing code.


    What I do is to use "typename" when any type can do as a parameter,
    and "class" when only a class should be used. Granted, the restriction
    isn't enforced by the language, but it's a nice, easy way to have the
    code self-document.
     
    red floyd, Dec 25, 2008
    #6
  7. Rui Maciel

    Rolf Magnus Guest

    red floyd wrote:

    > What I do is to use "typename" when any type can do as a parameter,
    > and "class" when only a class should be used. Granted, the restriction
    > isn't enforced by the language, but it's a nice, easy way to have the
    > code self-document.


    I don't really see a point in that. After all, if only a class can be used
    as a template parameter, that means that this class needs to support a very
    specific interface, which must be explicitly documented anyway.
     
    Rolf Magnus, Dec 25, 2008
    #7
    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,101
    psubramani
    Oct 20, 2012
  2. Replies:
    6
    Views:
    618
    Victor Bazarov
    Oct 20, 2005
  3. Victor Bazarov
    Replies:
    6
    Views:
    314
  4. Jakob Bieling

    Q: typename or not typename?

    Jakob Bieling, Mar 14, 2006, in forum: C++
    Replies:
    2
    Views:
    356
    Rolf Magnus
    Mar 14, 2006
  5. Replies:
    2
    Views:
    343
    Marc 'BlackJack' Rintsch
    Oct 25, 2008
Loading...

Share This Page