Difference...

What is the difference between the keywords class and typename in template declaretions?!?

eg:template<class T> and template<typename T>
Last edited on
closed account (zb0S216C)
There's 1 difference. From my point of view, typename implies more or a range of accepted types, whereas class implies only actual classes. However, the difference shows when you expect a template class as a template parameter. For instance:

1
2
3
4
5
// This is wrong:
template <template <typename> typename T>

// This is correct:
template <template <typename> class T>

Wazzak
Last edited on
There's none. I prefer typename since typname can be used outside the template directive to determine a template parameter if the compiler is confused. Look at this:

http://en.cppreference.com/w/cpp/keyword/typename
Last edited on
According to paragraph 2 of section 14.1 Template parameters of the C++ Standard

2 There is no semantic difference between class and typename in a template-parameter.


However when a template template-parameter is specified key-word class is used. For example

1
2
3
4
5
6
template <template <typename T = int, typename Allocator = std::allocator<T>>
          class Container = std::vector>
struct A
{
   Container c;
};
Last edited on
Thanks
Topic archived. No new replies allowed.