FUNCTION TEMPLATES Defining a Function Template Using a Function Template Overloading and Specializing Template Functions Defining a Function Template template Type sum (Type num1, Type num2) { return (num1 + num2); } void main () { int x, y, z; float xx, yy, zz; z = sum (x,y); zz = sum (xx,yy); } Function Template can be used to create sum functions for parameters of any type for which + operation is defined Keyword template introduces Function Template followed by a function template type parameter list In simplest case, only one function template type parameter is used Term class is not related to C++ classes Terms template and class must appear as shown (along with necessary brackets < and >) But Type could be any valid C++ name Temporary stand-in for built-in (int, float, char) or user-defined type If template function sum is created in which Type is int, function header will be int sum (int num1, int num2) If template function sum is created in which Type is float, function header will be float sum (float num1, float num2) If template function sum is created in which Type is a user-defined type very_big_number, function header will be very_big_number sum (very_big_number num1, very_big_number num2) Non-parameterized function template parameters... template void power (what_kind& pow, what_kind x, int n) { int i; what_kind term; term = (what_kind) 1; pow = (what_kind) 0; for (i=1; i<=n; i++) { term = term * x; pow = pow + term; } } Whatever type what_kind is, variable term will be declared to be of that type Cast is used to produce value of 1 of that type to store in term term = (what_kind) 1; x = (float) n; Can be more than one type in function template type parameter list template T1 bumble (T1 num1, T1 num2, T2& num3) { T1 temp; temp = num1 - num2; num3 = (T2) 0; return (temp); } remember: T1 bumble (T1 num1, T1 num2, T2& num3) void main () { float a; int x = bumble (7,4,a); int y; float b = bumble (16.3,9.4,y); int z; int w = bumble (7,4,z); } All types specified in function template type parameter list must appear in Function Template header // Caution - following does not work template T1 bumble (T1 num1, T1 num2, T2& num3) { T3 temp; temp = num1 - num2; num3 = (T2) 0; return (temp); } A Function Template can be declared as an inline function template inline Type sum (Type num1, Type num2) { return (num1 + num2); } Indicates to C++ compiler that any function sum created from this Function Template is potential inline function Inline directive does not appear on template line Using a Function Template Function Template tells C++ compiler how template function will look -- given actual type (or types) Creating Template Function from Function Template is called instantiation Type sum (Type num1, Type num2) int x; int y; int z; ... z = sum (x,y); Type is int Type sum (Type num1, Type num2) float xx; float yy; float zz; ... zz = sum (xx,yy); Type is float void power (what_kind& pow, what_kind x, int n) int x; int y; power (x,y,4); what_kind is int T1 bumble (T1 num1, T1 num2, T2& num3) int x; float a; x = bumble (7,4,a); T1 is int and T2 is float If formal type parameter occurs more than once, corresponding actual parameters must match in type template Type sum (Type num1, Type num2) { return (num1 + num2); } void main () { int x, z; float y; z = sum (x,y); // does not work } Overloading and Specializing Function Template can be overloaded template Type sum (Type num1, Type num2) { return (num1 + num2); } int sum (int num1, int num2, int num3) { return (num1 + num2 + num3); } Specializing Function Templates is overloading to provide function definition for types that will not work for standard algorithm or operators provided by Function Template template Type sum (Type num1, Type num2) { return (num1 + num2); } char* sum (char* a, char* b) { return (strcat (a,b)); } For two character strings, sum will return the concatenation (via string function strcat) of those strings