c++ – Are class and typename the same in the context of template?

Question:

I use:

template<class T>
Arbol<T>::Arbol(){

}

when T going to do a class.

and use:

template<typename T>
Arbol<T>::Arbol(){

}

when T going to be a type for example bool , char bone fundamental types.

But looking at a piece of code from a program (not created by me) I saw that it used typename where I usually use class , and until now I had not noticed that. If now you ask me why, well I couldn't say one hundred percent why, but perhaps somehow I (imagine) that the compiler differentiated when it was a type included in the language and acted accordingly.

My question is:

Is it the same to use one or the other?

Well, both now I have tested it and they work well but I don't know, if something is typename , or maybe there is some subtle difference between class and typename in this context.

Answer:

In this context, It is the same , there is no difference.

They can be used interchangeably and exist for historical reasons.

In this blog post by Stan Lippman, he explains that Stroustrup initially did not want to introduce a new keyword and reused class . Even the ISO-C ++ standard was the only way to declare it.

During the standardization process, it was discovered that this caused syntactic ambiguities (see on the blog) and it was decided to introduce a new keyword to solve it. this is typename . Finally class was overloaded to function as typename and thus maintain backwards compatibility.

Scroll to Top