Templates in C++

PHOTO EMBED

Tue Oct 03 2023 19:32:17 GMT+0000 (Coordinated Universal Time)

Saved by @ivo8871

// C++ Program to demonstrate
// Use of template
#include <iostream>
using namespace std;
 
// One function works for all data types.  This would work
// even for user defined types if operator '>' is overloaded
template <typename T> T myMax(T x, T y)
{
    return (x > y) ? x : y;
}
 
int main()
{
    // Call myMax for int
    cout << myMax<int>(3, 7) << endl;
    // call myMax for double
    cout << myMax<double>(3.0, 7.0) << endl;
    // call myMax for char
    cout << myMax<char>('g', 'e') << endl;
 
    return 0;
}
content_copyCOPY

https://www.geeksforgeeks.org/templates-cpp/