------------------------------------------------------------------------------
MC logo
Template Functions
[^] CSc 220 Home
------------------------------------------------------------------------------
[Introduction] [Boolean and Control] [Functions] [Arrays and Pointers] [Dynamic Arrays] [Array Errors] [Command Array] [Standard Containers] [Structs and Classes] [Automatic Pointer] [Multi-File Programs] [Copying Objects] [Templates] [Inheritance] [Plain C I/O] [Type Odds And Ends] [Plain C Strings] [File I/O]
[Template Functions] [Template Generalized Stack] [Template Generalized Stack Driver] [Array Template Stack] [Array Template Test Driver] [Generalized Safe Array] [Safe Array Demo] [Vector Iterator Demo]
minmax.cc
#include <iostream>
#include <string>

using namespace std;

// Template functions can create generic versions of functions.
template <typename T>
inline T dmin(const T a, const T b)
{
        if(a < b) return a;
        else return b;
}
template <typename T>
inline T dmax(const T a, const T b)
{
        if(a < b) return b;
        else return a;
}

main()
{
        cout << dmin(15, 46) << " " << dmax(58,32) << endl;
        cout << dmin(string("Fred"), string("Barney")) << " " << 
                dmax(string("Alice"), string("Sally")) << endl;
}