Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between Class and Typename for C++ template

I am slightly confused about c++ template.

Considering the template below

template<class TYPE>
void function(TYPE data)

and

template<typename TYPE>
void function(TYPE data)

My confusion is exactly what is the difference between typename and class used as variable identify or type.

like image 320
kcc__ Avatar asked Dec 06 '22 22:12

kcc__


1 Answers

For designating (type) template parameters, the two are exactly identical, just like int/signed int, or &&/and: template <typename>/template <class>.

A curious restriction applies to template template parameters up to C++14:

template <template <typename> class Tmpl> struct Foo;
//                            ^^^^^

Here only the keyword class is allowed to designate the template template parameter.

After C++14, you will be able to consistently use either class or typename everywhere:

template <template <typename> typename Tmpl> struct Foo;
like image 139
Kerrek SB Avatar answered Dec 31 '22 02:12

Kerrek SB