Why are you able to declare them using the class keyword or typename keyword? Is there absolutley no difference? Thanks

No. there is no diference whatsoever. i think that class was used originally when templates were added to C++ to avoid adding a new keyword, however eventually a new keyword was added anyway.

Anyone else wanna add there two cents? I prefer multiple reinforcment =D.

The only difference between both prototypes is the use of either the keyword class or the keyword typename. Its use is indistinct, since both expressions have exactly the same meaning and behave exactly the same way.

Bazzy

OK. I shall provide more information. In the context to which you refer where templates are defined e.g.

template<typename T>
void func(T& t ){}

or

template<class T>
void func(T& t ){}

There is no difference whatsoever, the keywords are totally interchangeable here.


An excerpt from the C++ programming language by Bjarne Stroustrup says...

The typename keyword can also be used as an alternative to class in template declarations:

template<typename T> void f(T);


However, typename does have other meanings in other contexts. in some cases the compiler is unable to distinguish whether a programmer intended to declare a type or do something else, like call a function. Take this example, also from the C++ programming language...

int y;

template<class T> void g(T& v)
{
     T::x(y); // function call or variable declaration?
}

Here the compiler is unablle to determine the intention...

Is T::x a function called with y as its argument? Or, did we intend to declare a local variable y with the type T::x perversely using redundant parentheses? We could imagine a context in which X::x(y) was a function call and Y::x(y) was a declaration. The resolution is simple: unless otherwise stated, an identifier is assumed to refer to something that is not a type or a template. If we want to state that something should be treated as a type, we can do so using the typename keyword:

template<class C> void h(C& v)
{
     typename C::iterator i = v.begin();
     // ...
}

The typename keyword can be placed in front of a qualified name to state that the entity named is a
type. In this, it resembles struct and class.
The typename keyword is required whenever a type name depends on a template parameter.

For example:

template<class T>
void k(vector<T>& v)
{
     vector<T>::iterator i = v.begin();  // syntax error: "typename" 
                                                           // missing
     typename vector<T>::iterator i = v.begin();    // ok
     // ...
}

In this case, a compiler might be able to determine that iterator was the name of a type in every instantiation of vector, but a compiler is not required to. Doing so would be a nonstandard and nonportable language extension. The only contexts where a compiler assumes that a name that depends on a template argument is a type name is in a few cases where only type names are allowed by the grammar.

There, all taken from 'The C++ Programming Language, Special Edition'.

From the horses mouth, you might say...

Sorry if its been said already.
The keyword class and typename when used in a template function has the same meaning, so they are interchangeable.

At first, there was only the keyword class for a template function. The
keyword class was used because people did not wan't to add any more new keyword( since it would be more to remember). But after a while, they decided that the usage of the keyword class when used in template function confused people as you see.

So they added the new keyword typename.It is more clear and I suggest you to use it if you can.

The only time you should use the keyword class in the context of template function is when you care about backward compatibility, since
some compiler does not implement the keyword typename.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.