// template3.cpp By: Aiman Hanna - ©1993-2006 Aiman Hanna // The program illustrates how the ambiguity issue is still present when // working with template functions. This program simply DOES NOT WORK. Why? // // Key Points: 1) template functions & ambiguity (ambiguous calls). #include // template functions that has one formal type, called "Type". The function // expects two parameters of type "Type". The function finds and returns the // smaller value of the two passed values. Hence, the return type of the // template function is also "Type" as well. template Type min( Type a, Type b ) { return a < b ? a : b; // same as: if ( a < b ) return a; else return b; } int main( ) { int x = 10, y = 5; double w = 13.2, z = 21.9; char ch1 = 'f', ch2 = 'k'; cout << " The minimum value of the two integers x and y is: " << min( x, y ) << endl << endl; cout << " The minimum value of the two doubles w and z is: " << min( w, z ) << endl << endl; cout << " The minimum value of the two characters ch1 and ch2 is: " << min(ch1, ch2) << endl << endl; // The following statement produces an ambiguous call to the template function cout << " The minimum value of the integer x and the double w is: " << min(x, w) << endl << endl; // This is line 39 return 0; } // The result of compiling the program // On UNIX /* CC: "template3.cpp", line 39: error: use of template function min() does not match any of its template definitions (1806) CC: "template3.cpp", line 39: error: ambiguous call: ostream* -> operator <<(any ) (1736) CC: "template3.cpp", line 39: choice of ostream::operator <<()s: CC: "template3.cpp", line 39: ostream& ostream::operator <<(char ); CC: "template3.cpp", line 39: ostream& ostream::operator <<(unsigned char ); CC: "template3.cpp", line 39: ostream& ostream::operator <<(const char *); CC: "template3.cpp", line 39: ostream& ostream::operator <<(int ); CC: "template3.cpp", line 39: ostream& ostream::operator <<(long ); CC: "template3.cpp", line 39: ostream& ostream::operator <<(double ); CC: "template3.cpp", line 39: ostream& ostream::operator <<(float ); CC: "template3.cpp", line 39: ostream& ostream::operator <<(unsigned int ); CC: "template3.cpp", line 39: ostream& ostream::operator <<(unsigned long ); CC: "template3.cpp", line 39: ostream& ostream::operator <<(void *); CC: "template3.cpp", line 39: ostream& ostream::operator <<(streambuf*); CC: "template3.cpp", line 39: ostream& ostream::operator <<(short ); CC: "template3.cpp", line 39: ostream& ostream::operator <<(unsigned short ); CC: "template3.cpp", line 39: ostream& ostream::operator <<(ostream&(*)(ostream&)); CC: "template3.cpp", line 39: ostream& ostream::operator <<(ios&(*)(ios&)); CC: "template3.cpp", line 39: error: bad argument 1 type for ostream::operator <<(): any (ios&(*)(ios&) expected) (1264) CC: "template3.cpp", line 39: error: function operand for << (1425) CC: "template3.cpp", line 39: error: function operand for << (1425) CC: "template3.cpp", line 39: error: function operand for << (1425) CC: "template3.cpp", line 39: error: function << ostream&(ostream&) (1420) */ // On Windows (VC++) /* Compiling... template3.cpp template3.cpp(39) : error C2782: 'template-parameter-1 __cdecl min(template-parameter-1,template-parameter-1)' : template parameter 'Type' is ambiguous could be 'double' or 'int' Error executing cl.exe. template1.exe - 1 error(s), 0 warning(s) */