Thread: typedef VS typename

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    53

    typedef VS typename

    Hi everyone ! I'm new to this forum ( but not completely new to programming ). Could anyone explain me what's the difference between typedef and typename ? I know that typedef will "create" a new type following a combination of keywords I give it, ex:
    Code:
    typedef unsigned int* UINT_PTR ;
    But what's the purpose of typename ? Is it when I create an entirely new type like a struct or a class ?

  2. #2
    Registered User
    Join Date
    Jul 2004
    Posts
    53
    Well, I've googled it and found that MSDN an explanation of it. Here's the complete article:
    typename
    C++ Specific
    typename identifier;
    Use this keyword only in template definitions. This keyword tells the compiler that an unknown identifier is a type. For example:
    Code:
    template<class T> class X {
      typename T::Y;    // treat Y as a type
      
      Y m_y;
    };
    This keyword can also be used in place of class in template parameter lists. For example, the following statements are identical:

    template<class T1, class T2>...
    template<typename T1, typename T2>...
    Could anyone explain me in what cases it would be useful or inevitable to use typename ?

  3. #3
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    typedef is to declare a type, typename is used to get the type which templates

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    I know of no place where you must use typename, only where you may use typename and where you can't use typename (see previous posts).

    My personal explanation for why typename exists as a keyword is so you can use any type, whether it is a primitive type, a user defined class, a user defined struct, (or a user defined union?) wherever you see typename. I assumed that if I used

    template<class T>

    that if I wanted to use a struct as T that I wouldn't be able to. Whether that is true or not, I don't know, as I've just used typename and let it go at that.

  5. #5
    Registered User
    Join Date
    Jul 2004
    Posts
    53
    So it could be used like this ?
    Code:
    //  myClass.h
    #ifndef __MYCLASS_H__
    #define __MYCLASS_H__
    
    #include <cstdio>
    #include <iostream>
    
    class myClass
    {
        public:
            typedef unsigned int* UINT_PTR ;
    } ;
    
    #endif
    Code:
    // foo.cpp
    #include "foo.h"
    #include "myClass.h"
    
    template< class T >
    void foo<T>::f( T& X )
    {
        typename X::UINT_PTR ;
    
        unsigned int blah = 0 ;
        UINT_PTR ptr = &blah ;
    
        std::cout << *ptr ;
    }

  6. #6
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Quote Originally Posted by elad
    I know of no place where you must use typename, only where you may use typename and where you can't use typename (see previous posts).
    Here's an example when you must use typename:
    Code:
    struct A
    {
    	typedef int Y;
    };
    template<class X>
    struct B
    {
    	typename X::Y y; //typename required here
    };
    If typename were omitted, the complier wouldn't be able to find out that X::Y was a type and give a syntax error.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  7. #7
    Compulsive Liar Robc's Avatar
    Join Date
    Jul 2004
    Posts
    149
    I got into a rather long discussion of how typename works with C+++C_forever. You can search for posts by Prelude with the keyword "typename".

  8. #8
    Registered User
    Join Date
    Jul 2004
    Posts
    53
    Quote Originally Posted by Sang-drax
    Here's an example when you must use typename:
    Code:
    struct A
    {
    	typedef int Y;
    };
    template<class X>
    struct B
    {
    	typename X::Y y; //typename required here
    };
    If typename were omitted, the complier wouldn't be able to find out that X::Y was a type and give a syntax error.
    So, from what I can see, I was correct ? Thank you for the infos guys, and I'll check that thread out.

  9. #9
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Quote Originally Posted by tilex
    So, from what I can see, I was correct ?
    Yes.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  10. #10
    Registered User
    Join Date
    Jul 2004
    Posts
    53
    Nice

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Need help understanding info in a header file
    By hicpics in forum C Programming
    Replies: 8
    Last Post: 12-02-2005, 12:36 PM
  3. Please STICKY this- vital to MSVC 6 dev - BASETSD.h
    By VirtualAce in forum Game Programming
    Replies: 11
    Last Post: 03-15-2005, 09:22 AM
  4. build errors migrated from dx9b to dx9c sdk
    By reanimated in forum Game Programming
    Replies: 4
    Last Post: 12-17-2004, 07:35 AM
  5. oh me oh my hash maps up the wazoo
    By DarkDays in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 12:54 PM