Create a template max() function that returns the greater of two types - C++ template

C++ examples for template:template function

Description

Create a template max() function that returns the greater of two types

Demo Code

#include <cstdio>
#include <cstdlib>
#include <iostream>

using namespace std;

template <class T> T maximum(T t1, T t2)
{
    return (t1 > t2) ? t1 : t2;
}

int main(int argc, char* pArgs[])
{
    cout << "maximum(-1, 2) = "<<maximum(-1, 2) << endl;

    cout << "maximum(1, 2.5) = "<<maximum<double>(1, 2.5) << endl;

    return 0;//from   w ww. ja v a2 s  .  c o m
}

Result


Related Tutorials