Click to See Complete Forum and Search --> : class keyword vs typename keyword in templates


JLafontaine
October 4th, 2002, 11:46 AM
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.:D

Yves M
October 4th, 2002, 12:06 PM
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)

Andreas Masur
October 4th, 2002, 01:41 PM
The prototype of class templates as well as function templates is any of the two following ones:

// 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)...

Graham
October 4th, 2002, 03:42 PM
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:

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.

Yves M
October 4th, 2002, 05:05 PM
Interesting :)

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

JLafontaine
October 5th, 2002, 08:56 AM
Thanks for the Info guys, I realy apreaciate it.

Amn
October 5th, 2002, 09:37 AM
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:


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.

Amn
October 5th, 2002, 09:44 AM
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.

Andreas Masur
October 5th, 2002, 11:54 AM
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...

Amn
October 5th, 2002, 01:23 PM
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 ! :D

Andreas Masur
October 5th, 2002, 04:32 PM
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 ! :D
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... :p

Graham
October 7th, 2002, 04:14 AM
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.