Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why must we do template <class/typename> T instead of just template T

Tags:

c++

templates

Instead of

template <typename T>
void func(T arg) {/* something */}

why can't we do

template <T>
void func(T arg) {/* something */}

From cplusplus.com :

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.

It just seems like unnecessary boilerplate to me.

like image 932
Avery3R Avatar asked Jul 03 '11 03:07

Avery3R


1 Answers

http://www.cplusplus.com/doc/tutorial/templates/ See the section on Non-type parameters for templates.

You need some keyword to distinguish type-parameters from non-type parameters.

template <class T, int N>
class mysequence {
    T memblock [N];
  public:
    void setmember (int x, T value);
    T getmember (int x);
};
like image 187
Justin Aquadro Avatar answered Sep 23 '22 08:09

Justin Aquadro