Thread: explanation of typename keyword?

  1. #1
    Registered User
    Join Date
    Apr 2003

    explanation of typename keyword?

    In "The C++ Standard Library" Josuttis gives this example(on p.11):
    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:
    #include <iostream> //cout
    using namespace std;
    class Box  //T = Box
    	static int SubType;
    //intialize static data member:
    int Box::SubType = 4;
    int main()
    	return 0;
    He continues his explanation by saying:

    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


    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:
    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
    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.

    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:

    My website:
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Waterloo, Texas
    >> 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:

    struct A 
     typedef int number;
    struct B : A 
     static int number;
    int B::number = 0;
    template <class T>
    struct C
      typename T::number n = 2;
      T::number = n;
     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.
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
        return std::pow
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;

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. 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, 08:22 PM