Reading Assignment


read the "Function templates" section
template functions





A template function is a short hand way of creating generic functions 

Since a template function has at least one parameter or return type that is unknown it is really not a standard function but instead
a "template" or recipe to create one.



given the following function template
T in this context is unknown and could be any type
it would match for 

string largerValue(string, string);
int largerValue(int, int);
char largerValue(char, char);
double largerValue(double, double);


template  // every template functions starts with this .. it defines T to be generic 
T largerValue(T input1, T input T)  // the return type, parameter1 and parameter2 are all the same 
{
if (input1 > input2)
  return input1;
else 
  return input2;
}



just this function template alone will not do much, but if we try to use the function in our code the compiler will try to use the template to build the appropriate verson that
will be needed



for example 
int main()
{
int result = largerValue(50, 100);  // this will compile a version of the largerValue fuction template. ALL occourances of 'T' will be replaced with int 

double a = 5.5;
double b = 9.9;

double c = largerValue(a,b); // this will compile a version of the largerValue function template. ALl occourances of  'T'  will be replaced with double


}

--------------------------------------------------------------------------------------------------------

the Prompt function in the cmpslib19.h is a good example of template function
it can be used to get input from the user and store it into many different datatypes

template 
inline void Prompt (string prompt, T & val)
{

	std::cout  << prompt; // display the text sent in as parameter to the console
	std::cin   >> val; // take what the user typed in and store it in the 2nd parameter

    while(std::cin.fail())  // if it fails... ie user input does not match the datatype
	{
	    // they entered in something that was not the proper data type so do the process over again 
		std::cin.clear();
		std::cin.ignore(std::numeric_limits::max(), '\n');
		std::cout  <<  prompt;
	    std::cin   >> val;
	}
}


the return type is void
the first parameter is a string, it should contain the text to display to the user before inputing text
the third paramter can be any type and this is where the input from the user will be stored.


int main()
{
int apples;
Prompt("How many apples would you like?", apples);
// apples now contains the value the user typed in

string dogs_name;
Prompt("What is the name of your dog?", dogs_name);

double dval; 
Prompt("How much does that bicycle cost?",dval);

}

when you try to compile this main it will then attempt to compile 3 versions of the Prompt function template, one where T is replaced with int, on where T is replaced with string
and one where T is replaced with double

------------------------------------------------------------------------------------------------------


here is a simple function that doesnt much but it has two template parameters

template 
void Print2Values(T & a, Z & b)
{
cout << "parameter 1 : value=" << a << " its address: " << & a << endl;
cout << "parameter 2 : value=" << b << " its address: " << & b << endl;
}



we could then use it like so 

Print2Values( 128, 22.44); // pass it an int and a double
Print2Values("hello", 44); // pass it a const char * and an int


---------------------------------------------------------------------------------------------------
potential problems
given the function template 

template 
T Product(T val1, T val2) 
{
return val1 * val2;
}


int result = Product(4,5);
// this will create a function with 2 int parameters and return the value of paramter 1 multiplied by parameter2

std::string temp = "hello";
std::string temp2= "world";

std::string result = Product(temp,temp2);
this will try to create a function that will multiply the 2 strings ... but you cant multiply strings....
so you will get an error with the message saying no match for operator * 
this means that it does not understand   "return val1 * val2"  becasue string * string is not valid