Why the "typename" in iterator templates?

This is a discussion on Why the "typename" in iterator templates? within the C++ Programming forums, part of the General Programming Boards category; Lets say you have the following: Code: template<class T> void doSomething(std::list<T> myList) { std::list<T>::iterator i; } Won't work. You need ...

  1. #1
    Amazingly beautiful user.
    Join Date
    Jul 2005
    Location
    If you knew I'd have to kill you
    Posts
    254

    Why the "typename" in iterator templates?

    Lets say you have the following:
    Code:
    template<class T>
    void doSomething(std::list<T> myList)
    {
      std::list<T>::iterator i;
    }
    Won't work. You need
    Code:
    template<class T>
    void doSomething(std::list<T> myList)
    {
      typename std::list<T>::iterator i;
    }
    I'm assuming that for some reason the meaning of "std::list<T>::iterator" is ambiguous, so you need to tell the compiler you are using it as a typename, but why does using the templates make it ambiguous? What else could it mean to the compiler?
    Programming Your Mom. http://www.dandongs.com/

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Why not check out the first google hit for "typename"?
    http://publib.boulder.ibm.com/infoce...d_typename.htm
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Amazingly beautiful user.
    Join Date
    Jul 2005
    Location
    If you knew I'd have to kill you
    Posts
    254
    Sorry. My searches were too elaborate as I figured it was a problem specific towards iterators, and the only results I got were those of people posting problems similar to my own, which contained little explanation, only the solution. Thank you for the response.
    Programming Your Mom. http://www.dandongs.com/

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21