// test_template.cpp define and use a function template // define a template template // user chooses a type for 'typ' typ funct( typ x, typ y, typ z) // the function takes 3 parameters { typ a, b; a = x + y; // our template limits the users b = y - z; // choice for 'typ' to a type return a * a / b; // that has +, -, *, and / defined } #include // 'typ' must also have operator << defined using namespace std; int main() { int i = funct(3, 5, 7); // integer for typ long int j = funct(3L, 5L, 7L); // long int for typ unsigned int k = funct(3U, 5U, 7U); // unsigned int for typ float x = funct(3.1F, 5.2F, 7.3F); // float for typ double y = funct(3.1, 5.2, 7.3); // double for typ cout << i << " int, " << j << " long, " << k << " unsigned, " << x << " float, " << y << " double.\n"; return 0; } // end main // output is // -32 int, -32 long, 0 unsigned, -32.8047 float, -32.8048 double. // note the very different answer for the unsigned type // remember a small negative number becomes a very large positive number