explanation of typename keyword?

This is a discussion on explanation of typename keyword? within the C++ Programming forums, part of the General Programming Boards category; In "The C++ Standard Library" Josuttis gives this example(on p.11): Code: template <class T> class MyClass{ typename T::SubType * ptr; ...

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662

    explanation of typename keyword?

    In "The C++ Standard Library" Josuttis gives this example(on p.11):
    Code:
    template <class T>
    class MyClass{
    	typename T::SubType * ptr;
    	...
    };
    He says that without the typename keyword, SubType would be considered a static member. Ok, that seems to make some sense because a static member can be referenced like this:
    Code:
    #include <iostream> //cout
    using namespace std;
    
    class Box  //T = Box
    {
    public:
    	static int SubType;
    };
    
    //intialize static data member:
    int Box::SubType = 4;
    
    int main()
    {
    	cout<<Box::SubType<<endl;
    
    	return 0;
    }
    He continues his explanation by saying:
    Thus,

    T::SubType * ptr;

    would be a multiplication of value SubType of type T with ptr.
    I don't understand why he says that SubType is a value of type T? It seems to me that when you write

    T::SubType

    that only says that SubType is a static member of T, and it doesn't speak to the type of SubType at all. In my example above, the statement:
    Code:
    cout<<Box::SubType<<endl;
    does not say that SubType is of type Box. It just says to go look in the Box class for a static member variable called SubType. The type of SubType is actually int.

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    You need the typename keyword when you use a type that's declared in a class that hasn't been defined yet. A type, not a variable.
    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
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,655
    >> I don't understand why he says that SubType is a value of type T?

    I think he means that Subtype is a value within the class T.
    here's another example of how it's supposed to work:

    Code:
    struct A 
    {
     typedef int number;
    };
    
    struct B : A 
    {
     static int number;
    };
    
    int B::number = 0;
    
    template <class T>
    struct C
    { 
     C()
     {  
      typename T::number n = 2;
      T::number = n;
     }
    };
    
    int
    main(void)
    {
     C<B> c; 
    };
    oddly enough, that code chokes the Borland compiler - but it should work for you if you're using a standard-compliant one.

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, 12:06 PM
  2. Need Partners (D&D fan preferably)
    By C_ntua in forum Game Programming
    Replies: 44
    Last Post: 11-22-2008, 08:21 AM
  3. typedef and typename
    By VirtualAce in forum C++ Programming
    Replies: 16
    Last Post: 07-17-2008, 08:11 PM
  4. about typename keyword
    By sawer in forum C++ Programming
    Replies: 3
    Last Post: 04-22-2006, 05:44 AM
  5. how to search a keyword in C?
    By kreyes in forum C Programming
    Replies: 2
    Last Post: 03-01-2002, 07:22 PM

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