what are the differences between typename and class

This is a discussion on what are the differences between typename and class within the C++ Programming forums, part of the General Programming Boards category; The following code work exactly the same no matter I use the keyword typename or class. Code: template < typename ...

  1. #1
    C/C++Newbie Antigloss's Avatar
    Join Date
    May 2005
    Posts
    212

    what are the differences between typename and class

    The following code work exactly the same no matter I use the keyword typename or class.
    Code:
    template < typename my_type >
    my_type fun( my_type arg1, my_type arg2 )
    {
    	return arg1 + arg2;
    }
    What are the differences between these keywords when used to define generic functions?
    Last edited by Antigloss; 09-02-2005 at 08:12 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,598
    As far as I know, there is no difference for this use. I have read of a coding style convention that uses class for template parameters that are strictly user defined, and typename for those that can take primitive types.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,252
    As laserlight says, in the example given, there is no difference between using typename or class keywords.

    There are cases when typename is necessary (and the class keyword can't be used instead), such as;
    Code:
    template<class T> class Y {
                    T::A(b);               // error
            };
    This is an error, as the compiler (when it parses this bit of code) has no way of working out if T::A is a type or if it is something else. In this example, T::A(b) might be a call of a function named T::A, passing a global named b as an argument. It might also be (in an allowed, but rarely used form) a declaration of a variable b that is of type T::A. The basic point is that it is difficult for a compiler to make the distinction when parsing template code --- the point it will have that information comes later, when the template is instantiated (i.e. used for a particular type T, in this example). The typename keyword is used to avoid confusing the compiler at a point where it has insufficient information;
    Code:
    template<class T> class Y {
                   typename  T::A(b);               // error
            };
    which tells the parser that T::A will be (for some class T) a type, so the declaration is of something named b.

  4. #4
    Nonconformist Narf's Avatar
    Join Date
    Aug 2005
    Posts
    174
    What are the differences between these keywords when used to define generic functions?
    The only difference I've found is when you declare a template template:
    Code:
    // Works fine
    template <template <typename T> class U>
    void foo() {
    }
    In this code, class is not interchangeable with typename, and if you try then it won't compile:
    Code:
    // Won't compile
    template <template <typename T> typename U>
    void foo() {
    }
    Just because I don't care doesn't mean I don't understand.

  5. #5
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    A side note on grumpy's:

    Consider:
    Code:
    struct foo {
       typedef my_traits<foo>::char_t* ptr_t;
    };
    It is clear that you want the type my_traits<foo>::char_t* to be typedef'd as ptr_t, but the compiler seeing this, will think that my_traits<foo>::char_t is a static member of the class (even in cases for which this makes no sense, the compiler has no way of really knowing this).

    Typename helps it along:
    Code:
    struct foo {
       typedef typename my_traits<foo>::char_t* ptr_t;
    };
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need Partners (D&D fan preferably)
    By C_ntua in forum Game Programming
    Replies: 44
    Last Post: 11-22-2008, 08:21 AM
  2. Specializing class
    By Elysia in forum C++ Programming
    Replies: 6
    Last Post: 09-28-2008, 04:30 AM
  3. typedef and typename
    By VirtualAce in forum C++ Programming
    Replies: 16
    Last Post: 07-17-2008, 08:11 PM
  4. Default class template problem
    By Elysia in forum C++ Programming
    Replies: 5
    Last Post: 07-11-2008, 08:44 AM
  5. typename in class template
    By l2u in forum C++ Programming
    Replies: 4
    Last Post: 04-02-2008, 04:22 PM

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