However, in the exercise accompanying this lesson, in place of void for the function, it is T.
Can you use void as well or in order for a template to work should you also use T there?
Thank you for any help!
A template defines a generic type - we don’t know what type it will be
template < typename T>
T add(T a, T b) {
return a+b;
}
template <typename T>
void print_add(T a, T b) {
std::cout << a+b;
}
T means some sort of generic data type - could be an integer, a float, a character
void means - this function does not return any sort of data type!
the function add takes two generic arguments a and b
add(3,4)
Compiler: ok two integers - lets add them and return an integer
it returns the implied datatype integer with the value 7
add(3.0, 4.0)
Compiler: ok two floats - lets add them and return a float
it returns the implied datatype float with the value 7.0
the function print_add is Void it doesn’t return any value it just prints the result of adding two T’s to the screen