<<< Function templates | Index | Templates and friend >>> |
Type ValueT will be deduced from the actual call parameters.
Here, max_val( ) requires availability of operator> for ValueT:
#include <iostream> #include <string> // return bigger ValueT template< typename ValueT > ValueT const& max_val( ValueT const& a, ValueT const& b ) { return ( a > b ) ? a : b; } int main() { using namespace std; string str_a( "hello" ); string str_b( "bye" ); string bigger = max_val( str_a, str_b ); cout << bigger; return 0; } // Program prints "hello"
<<< Function templates | Index | Templates and friend >>> |