typename

Discussion in 'C++' started by saneman, Jan 29, 2008.

  1. saneman

    saneman Guest

    In a template class I have:

    typedef std::list<V,A> list;
    typedef list::iterator iterator;

    but this gives a compile error. If I replace the second line with:

    typedef typename list::iterator iterator;

    it works. But why? I have read that typename should be used when an
    expression depends on template parameters.

    If that is the case why is it not necessary to use typename in the first
    line?
    saneman, Jan 29, 2008
    #1
    1. Advertising

  2. saneman wrote:
    > In a template class I have:
    >
    > typedef std::list<V,A> list;
    > typedef list::iterator iterator;
    >
    > but this gives a compile error. If I replace the second line with:
    >
    > typedef typename list::iterator iterator;
    >
    > it works. But why? I have read that typename should be used when an
    > expression depends on template parameters.
    >
    > If that is the case why is it not necessary to use typename in the first
    > line?


    Because the first line is always a class and hence always a type so no
    typename is required. In the second case. std::list<V,A> may be a
    specialization and hence iterator could literally be anything so you're
    telling the compiler that it really is a type. The reason you need to
    do that is because the c++ grammer becomes ambiguous otherwise.
    Gianni Mariani, Jan 29, 2008
    #2
    1. Advertising

  3. * saneman:
    > In a template class I have:
    >
    > typedef std::list<V,A> list;
    > typedef list::iterator iterator;
    >
    > but this gives a compile error. If I replace the second line with:
    >
    > typedef typename list::iterator iterator;
    >
    > it works. But why? I have read that typename should be used when an
    > expression depends on template parameters.
    >
    > If that is the case why is it not necessary to use typename in the first
    > line?


    In the first line std::list<V,A> will be a concrete a concrete class
    when the template parameters are filled in, and thus known to be a type.

    It's unknown exactly /which/ concrete class std::list<V,A> will be, but
    it's known that it will be some concrete class.

    The second line is equivalent to

    typedef std::list<V,A>::iterator iterator;

    Here, due to the lack of knowledge of exactly which concrete class
    std::list<V,A> will be, e.g. due to specialization of the std::list
    template, it's unknown whether there will be an 'iterator' member and if
    so, what kind of member it will be -- type, variable, function,
    template, what?

    You have to say what you assume and require that it will be.

    In effect, by adding the word 'typename' you make a specialization of
    std::list that doesn't have a type 'iterator', invalid.


    Cheers & hth.,

    - Alf

    --
    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, Jan 29, 2008
    #3
  4. saneman wrote:

    > In a template class I have:
    >
    > typedef std::list<V,A> list;
    > typedef list::iterator iterator;
    >
    > but this gives a compile error. If I replace the second line with:
    >
    > typedef typename list::iterator iterator;
    >
    > it works. But why? I have read that typename should be used when an
    > expression depends on template parameters.
    > ...


    You haven't provided enough context, but from what you said already I
    can make an educated guess about either 'V' or 'A' (or both) being a
    template parameter, not a concrete types. If that's true, then type
    'list' is still a dependent type, not a concrete type, i.e. it depends
    on the template parameters. Just because you have "hidden" that
    dependency behind a typedef name doesn't really change anything. You
    still need a 'typename' when referring to nested typenames.

    --
    Best regards,
    Andrey Tarasevich
    Andrey Tarasevich, Jan 29, 2008
    #4
    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. Xavier Decoret

    Gcc complain about typename

    Xavier Decoret, Jul 4, 2003, in forum: C++
    Replies:
    1
    Views:
    491
    Dhruv
    Jul 4, 2003
  2. Gina Yarmel

    typename, typedef, and resolution

    Gina Yarmel, Aug 13, 2003, in forum: C++
    Replies:
    4
    Views:
    4,744
    Sergei Emantayev
    Aug 13, 2003
  3. Steve Hill

    implicit typename in template

    Steve Hill, Sep 6, 2003, in forum: C++
    Replies:
    1
    Views:
    428
    ES Kim
    Sep 6, 2003
  4. Alexander Malkis

    class vs. typename

    Alexander Malkis, Apr 6, 2004, in forum: C++
    Replies:
    7
    Views:
    13,048
    psubramani
    Oct 20, 2012
  5. Jakob Bieling

    Q: typename or not typename?

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

Share This Page