syntax for function template

S

subramanian100in

Consider

template <typename T>
void compare_and_print(const T & arg1, const T & arg2)

template <class T>
void compare_and_print(const T & arg1, const T & arg2)

If I use any one of these two, the compiler accepts it. That is, the
compiler accepts both "typename T" and "class T" inside the angle
brackets <>. Which form is correct ?

Kindly explain.

Thanks
V.Subramanian
 
K

Kai-Uwe Bux

Consider

template <typename T>
void compare_and_print(const T & arg1, const T & arg2)

template <class T>
void compare_and_print(const T & arg1, const T & arg2)

If I use any one of these two, the compiler accepts it. That is, the
compiler accepts both "typename T" and "class T" inside the angle
brackets <>. Which form is correct ?

Both are correct. Moreover, they mean the same thing.

One can argue that "class T" is archaic and misleading (it seems to suggest
that built-in types are not allowed as arguments). In any case, which one
to use is a matter of style and not a matter of correctness.


Best

Kai-Uwe Bux
 
I

Ian Collins

Consider

template <typename T>
void compare_and_print(const T & arg1, const T & arg2)

template <class T>
void compare_and_print(const T & arg1, const T & arg2)

If I use any one of these two, the compiler accepts it. That is, the
compiler accepts both "typename T" and "class T" inside the angle
brackets <>. Which form is correct ?
Both.

"class T" was the pre-standard way of specifying a template parameter,
"typename T" was added because a template parameter is a typename, not
necessarily a class.
 
J

James Kanze

"class T" was the pre-standard way of specifying a template
parameter, "typename T" was added because a template parameter
is a typename, not necessarily a class.

Just a nit, but what actually happened is that class was
originally used, to avoid introducing yet another keyword. When
the keyword typename was introduced for dependent names, it was
decided that it would also be appropriate to allow it here.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,270
Messages
2,571,353
Members
48,041
Latest member
Oliwen826

Latest Threads

Top