is template T a class with triple as a member function |
This line:
template <typename T>
is a message to the compiler, telling it the next function is a
template function where "T" is a "place-holder type", for lack of a better term.
this definition (call it definition 'A'):
1 2 3 4 5
|
template <typename T>
T triple(T v)
{
return v * 3;
}
|
is a place-holder for functions that will be generated at compile time.
Say, that your code contains the following calls to "triple" somewhere in the code:
1 2 3 4 5
|
int tripleInt = triple(5);
...
float tripleFloat = triple(6.2);
...
double tripleDouble = triple(3.2);
|
then the compiler will expand 'A' to:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
int triple(int v)
{
return v * 3;
}
float triple(float v)
{
return v * 3;
}
double triple(double v)
{
return v * 3;
}
|
If you only call
int tripleInt = triple(5);
in your code, then only the
int
version of the function would be generated.
Of course, this generation happens automatically. It's easier to think of "triple", as Disch said, as a function that could take any type as a parameter (so long as you can multiply it by 3).
does not triple only have one argument |
Yes, it has one argument ("v" of type "T").