I've seen tutorials, I've read books, and I still don't understand what a template is. I know it's to accept all variable types but I don't know how to implement them and use them. If the little I know of them is correct how would I made the following add() function a template and how would I use it?
If you make a function a template... then it's not really a function on it's own. Instead it is a blueprint for which the compiler can make any number of different functions.
For example... with normal functions, you could repeat the function several times:
// a function that works with ints
int add_int(int x, int y)
{
return (x+y);
}
// one that works with doubles
double add_double(double x, double y)
{
return (x+y);
}
// one that works with strings (though 'add' is a bit weird)
std::string add_string(std::string x, std::string y)
{
return (x+y);
}
// using all of them:
int main()
{
cout << add_int( 5, 10 ); // prints 15
cout << add_double( 1.23, 3.5 ); // prints 4.73
cout << add_string( "hi", " there" ); // prints "hi there"
}
Notice how all 3 of these functions are different. add_int only works with ints. add_double only works with doubles, etc.
Also notice that even though all the functions are different... the body for them is all the same:
1 2 3
{
return (x+y);
}
So... instead of duplicating this function a dozen times so that it will exist for all the different types we want to be able to add... we can just create a template that the compiler can use to create those functions for us.
template <typename T> // <- 'T' can be whatever type.
T add(T x, T y) // <- so if T==int, then this would be 'int add(int x, int y)'
{ // ... or if T==string, then this would be 'string add(string x, string y)'
return (x+y); // etc
}
// Now that the template is written... we can use it to create any number of functions for us automatically.
// we don't have to manually create each function.
int main()
{
cout << sum<int>( 5, 10 ); // <- calls add where T==int. Prints 15
cout << sum<double>( 1.23, 3.5 ); // <- calls add where T==double. Prints 4.73
cout << sum<string>( "hi", " there" ); // <- calls add where T==string. Prints "hi there"
/*
As you can see... we determine what 'T' is by putting it in angle brackets. IE
sum<int> makes T==int.
However... we often don't even need to do that. The compiler can usually figure out what T is
based on the parameters you give it:
*/
cout << sum( 5, 10 ); // <- since 5 and 10 are both ints... it knows T==int. We don't have to tell it.
cout << sum( 1.23, 3.5 ); // <- since 1.23 and 3.5 are both doubles... it knows T==double.
std::string a = "hi";
std::string b = " there";
cout << sum( a, b ); // <- since a and b are both strings... it knows T==string
}
No, for classes template argument deduction is not allowed. YOu have to write std::vector<int>, you have to write SayHi<string> in your templated class.
1 2 3 4 5 6
SayHi<string> sayHi("SUP!");
sayHi.display();
SayHi<int> sayHi(42);
sayHi.display();
//SayHi<string> sayHi(0);//Will not compile:
//sayHi.display(); //int cannot be converted to string
SUP!
42
If you ever used vector, you used templated class. string is actually is a typedef for std::basic_string<char>, so it is a (hidden) templated class too