typename vs class in C++

If you are a C++ programmer, you might have wondered about this question at some point. Is there really any difference between using typename and class when declaring template parameters? Does it matter which one you choose? Does it affect the performance or readability of your code?

Well, I have good news for you! The answer is NO! There is absolutely no difference between typename and class for naming template parameters. They are completely interchangeable and equivalent. You can use whichever one you like, or even mix them up if you want to (although I wouldn’t recommend that for consistency reasons).

typename however is possible in another context when using templates - to hint at the compiler that you are referring to a dependent type. ยง14.6.2:

A name used in a template declaration or definition and that is dependent on a template-parameter is assumed not to name a type unless the applicable name lookup finds a type name or the name is qualified by the keyword typename.

But, pre-C++98 only class was allowed, but that was a long time ago!

This means that you can write something like this:

template <typename T>
void foo(T x) {
	// do something with x
}

template <class U>
void bar(U y) {
	// do something with y
}

And it will work exactly the same as if you wrote this:

template <class T>
void foo(T x) {
	// do something with x
}

template <typename U>
void bar(U y) {
	// do something with y
}

Isn’t that awesome? You don’t have to worry about which keyword to use, or memorize any obscure rules or exceptions. You can just focus on writing your awesome template code and let the compiler handle the rest.

But wait, there’s more! There is another context where typename can be used in C++, and that is when you need to tell the compiler that you are referring to a dependent type. What does that mean? Well, let me explain.

A dependent type is a type that depends on a template parameter. For example, if you have a template class like this:

template <typename T>
class Foo {
public:
	typedef T value_type;
	// other stuff
};

Then Foo<T>::value_type is a dependent type, because it depends on what T is. Now, suppose you want to write another template function that takes an argument of type Foo<T>::value_type. How would you write it?

You might be tempted to write something like this:

template <typename T>
void baz(Foo<T>::value_type z) {
	// do something with z
}

But this won’t work! The compiler will complain that Foo<T>::value_type is not a type name. Why? Because the compiler doesn’t know what T is at this point, so it can’t resolve what Foo<T>::value_type means. It could be anything: a variable name, a function name, an enum value, etc.

So how do we fix this? We need to use the keyword typename before Foo<T>::value_type, like this:

template <typename T>
void baz(typename Foo<T>::value_type z) {
	// do something with z
}

This tells the compiler that we are referring to a type name here, not anything else. This way, the compiler can parse our code correctly and generate the appropriate template instantiation.

This is called disambiguation of dependent types. It’s only necessary when we are using nested types inside templates. It’s not needed for simple types like int or string.

So there you have it! The only difference between typename and class in C++ is when we need to disambiguate dependent types inside templates. Otherwise, they are exactly the same and interchangeable.

I hope this blog post has cleared up any confusion you might have had about this topic. If you enjoyed reading it, please share it with your friends and colleagues who might also benefit from learning more about C++. And don’t forget to subscribe to my blog for more awesome C++ tips and tricks!

Happy coding!


To contact me, send an email anytime or leave a comment below.