CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12
  1. #1
    Join Date
    Oct 2002
    Location
    Canada
    Posts
    9

    class keyword vs typename keyword in templates

    I have noticed that when writing template some people use
    template <typename T>
    and others
    template <class T>
    what is the difference, if any, between using typename and class.

    Me personaly I've alwas used the class keyword.

  2. #2
    Join Date
    Aug 2002
    Location
    Madrid
    Posts
    4,588
    I don't have a clue either. Using VC6, using typename or class seems to produce the same results. (If I pass a custom class to a template <typename T> it doesn't complain)

  3. #3
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652
    The prototype of class templates as well as function templates is any of the two following ones:
    Code:
    // Function template
    template<class identifier> function_declaration;
    template<typename identifier> function_declaration;
    
    
    // Class template
    template<class identifier>
    class CFoo
    {
    };
    
    template<typename identifier>
    class CFoo
    {
    };
    The only difference between both prototypes is the use of keyword class or typename, its use is indistinct since both expressions have exactly the same meaning and behave exactly the same way.

    As you see at the end it is just user's choice. I usually prefer 'typename' to indicate that not necessarely a class gets passed (what some people might assume by the keyword class)...
    Last edited by Andreas Masur; October 4th, 2002 at 01:43 PM.

  4. #4
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470
    No difference at all. Like Andreas, I use "typename" if basic types such as int and pointers are valid, but I use class if there are special requirements that can't be met by the basic types.

    For example, I would probably declare std::vector as:
    Code:
    template <typename T, class Alloc> class vector;
    T has very few restrictions (it must be properly copyable is about all), so ints, floats, pointers, structs can all be used for T. Alloc, however, has very special requirements and can only be a class.

    This is purely stylistic, though - nothing enforces it.
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  5. #5
    Join Date
    Aug 2002
    Location
    Madrid
    Posts
    4,588
    Interesting

    It's true that using typename might be clearer in some cases.

  6. #6
    Join Date
    Oct 2002
    Location
    Canada
    Posts
    9
    Thanks for the Info guys, I realy apreaciate it.

  7. #7
    Join Date
    May 2001
    Location
    Oslo, Norway
    Posts
    610
    I recall there were some differences when class keyword wouldn't do and typaneme is needed. Not in template definition, but still:

    * Searches for the C++ article *

    Here it is:

    Code:
    template<class T> class X
    {
    
    	// Without typename, you should get an error:
    	typename T::id i;
    
    public:
    
    	void f() { i.g(); }
    };
    
    class Y
    {
    public:
    
    	class id
    	{
    	public:
    		void g() {}
    	};
    
    };
    
    int main()
    {
    	Y yObj;
    	X<yObj> xObj;
    	xObj.f();
    }
    Consider this:

    compiler hits the T::id and ambiguity rises: is id a member or nested class ? typename assures the compiler that id is indeed a nested class and NOT a member.

    Be carefull when giving answers, the person gets away with false knowledge and whats worse he will probably never return here to check some additional posts.
    Last edited by Amn; October 5th, 2002 at 09:42 AM.

  8. #8
    Join Date
    May 2001
    Location
    Oslo, Norway
    Posts
    610
    Standard says however that in template argument definition the keyword 'typename' is synonimical to 'class'. It was introduced to standard much later than the '93 revision, and some old c++ compilers dont support it.

  9. #9
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652
    Originally posted by Amn
    Be carefull when giving answers, the person gets away with false knowledge and whats worse he will probably never return here to check some additional posts.
    Well...I do not see any post that did not completely answer the question. The question was about the usage of 'class' and 'typename' within the template declaration. In this context as I said it is just user's choice.

    You are referring to something else which is of course right but serves a different purpose and which might confuse the OP as well. Do not get me wrong, your information is correct and useful but nevertheless the OP did not get away with false knowledge though even without your additional information...

  10. #10
    Join Date
    May 2001
    Location
    Oslo, Norway
    Posts
    610
    Originally posted by Andreas Masur

    Well...I do not see any post that did not completely answer the question. The question was about the usage of 'class' and 'typename' within the template declaration. In this context as I said it is just user's choice.

    You are referring to something else which is of course right but serves a different purpose and which might confuse the OP as well. Do not get me wrong, your information is correct and useful but nevertheless the OP did not get away with false knowledge though even without your additional information...
    True, Andres true

    But i payed attention to

    'what is the difference, if any, between using typename and class.' as posted by JLafontaine

    And since keyword is a keyword no matter which context it appears in, i thought i'd clear that out.

    And you know one cant be too careful !

  11. #11
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652
    Originally posted by Amn
    But i payed attention to

    'what is the difference, if any, between using typename and class.' as posted by JLafontaine

    And since keyword is a keyword no matter which context it appears in, i thought i'd clear that out.

    And you know one cant be too careful !
    Yes, you are right. But as I said in my earlier post, it was not meant as critism about the useful information you provided. I thought about this as well while writing my answer but decided that this would lead too far...well maybe I was just too lazy to write it all down...

  12. #12
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470
    The context of the original post was clearly about template declarations. Introducing the side-issue of typename's use to clarify a dependent type in a variable declaration would, I feel, have muddied the issue. A bit like pointing out that the < and > in the template definition are also used as less than and greater than operators - true and complete, but not relevant.
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured