If the template parameters had more descriptive names, they could look like this:
template <class ReturnType, class ParameterType>
ReturnType tripleit (ParameterType val)
{
ReturnType temp = val * 3;
return temp; // I assume this line is actually present in your code, it makes no sense otherwise
}
With these names, it should be a bit more clear. The function can be used to multiply a number by 3 and convert it to a desired type at the same time.
Invoking the template with both template arguments specified simply suppresses template argument deduction. I think the really interesting case is missing there:
cout << "Explicit double, int; double argument: " << tripleit<double, int>(d) << '\n';
This will pass in a double
value 3.3
. However, as ParameterType
is explicitly specified to be int
, the value will be converted to int
(perhaps with a warning). Inside the function, temp
will be of type double
(the first template argument), but the return value will still be 9, so the expected output is 9
or perhaps 9.0
or 9.0e0
depending on the current cout
settings for floating-point numbers.