CPP
22
max 2 cpp
Guest on 5th July 2022 05:10:55 PM
// max.cpp
#include<iostream>
using namespace std;
// max(): maximum of two values of any type
template<typename T>
T const& max (T const& a, T const& b)
{
return a < b ? b : a;
}
// max(): maximum of three values of any type
template <typename T>
T const& max (T const& a, T const& b, T const& c)
{
return ::max (::max(a,b), c);
}
int main()
{
cout << ::max(7, 42, 68) << endl; // calls 3 argument version
cout << ::max(7.0, 42.0) << endl; // calls max<double>
cout << ::max('a', 'b') << endl; // calls max<char>
cout << ::max(7, 42) << endl; // calls max<int>
cout << ::max<double>(7, 42) << endl; // calls max<double>
return 0;
}
// output:
//
// 68
// 42
// b
// 42
// 42