Thread: about typename keyword

  1. #1
    Registered User
    Join Date
    Jan 2006

    about typename keyword

    I am reading thinking in c++ book(Vol.2).I am in template section amd i don't understand typename keyword's meaning and usage.

    Here is sample code:
    #include <iostream>
    #include <list>
    #include <memory>
    #include <vector>
    using namespace std;
    template<class T, template<class U, class = allocator<U> >
             class Seq>
    void printSeq(Seq<T>& seq) {
      for(typename Seq<T>::iterator b = seq.begin();
           b != seq.end();)
        cout << *b++ << endl;
    int main() {
      // Process a vector
      vector<int> v;
      // Process a list
      list<int> lst;
    } ///:~
    Here why do we write "typename Seq<T>::iterator b" .Maybe i don't understand because Seq<T> is special library.Is it a type of library?

    I really don not understand typename keyword.

    Can someone explain me that please?

  2. #2
    Registered User fischerandom's Avatar
    Join Date
    Aug 2005
    In general it is not possible for the non-psychic compiler to know that Seq<T>::iterator is intended as the name of a type (type name) rather than the name of something that is not a type (such as a function or a template). If we want to state that something should be treated as a type, we can do so using the typename keyword. For more details you can read a C++ standard text. I got the above information from The C++ Programming Language, Special Edition by Stroustrup, the Creator of C++, section C.13.5 Typename and Template.
    Bobby Fischer Live Radio Interviews

  3. #3
    Registered User
    Join Date
    Apr 2003
    I am in template section amd i don't understand typename keyword's meaning and usage.
    Suppose you had a static member of the class Seq<T> called 'iterator' and it was equal to 10. You could display iterator's value like this:


    So in this statement:

    Seq<T>::iterator b = seq.begin();

    the compiler thinks Seq<T>::iterator is a variable name, and it tries to replace it with its value. If there were a static variable in the class Seq<T> called iterator, and it had a value of 10, that would produce:

    10 b = seq.begin();

    Even though that doesn't make any sense, the compiler still can't figure out that in that context Seq<T>::iterator should not be considered a static variable name. So to tell the compiler that you are not refering to a static variable name, you need to use the keyword 'typename'. The typename keyword tells the compiler to look in Seq<T> for a type called 'iterator' rather than a variable called 'iterator'.
    Last edited by 7stud; 04-22-2006 at 04:50 AM.

  4. #4
    Registered User
    Join Date
    Jan 2006
    Ok friends ,now i understand.
    Thanks fischerandom.
    7stud it is very good description, thank you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Use of a class in place of main( )?
    By Sebastiani in forum C++ Programming
    Replies: 15
    Last Post: 12-10-2008, 01:06 PM
  2. Need Partners (D&D fan preferably)
    By C_ntua in forum Game Programming
    Replies: 44
    Last Post: 11-22-2008, 09:21 AM
  3. typedef and typename
    By VirtualAce in forum C++ Programming
    Replies: 16
    Last Post: 07-17-2008, 08:11 PM
  4. explanation of typename keyword?
    By 7stud in forum C++ Programming
    Replies: 2
    Last Post: 02-18-2006, 05:05 PM
  5. how to search a keyword in C?
    By kreyes in forum C Programming
    Replies: 2
    Last Post: 03-01-2002, 08:22 PM