#include <iostream> #include <string> using namespace std; template < class T> T Max(const T &x,const T &y) { return (x < y ? y:x); } int main() { double a=3.2,b=4.2; int c=4,d=7; string s="Hallo",t="Hello"; cout << "Max(a,b) :" << Max(a,b) << endl; cout << "Max(c,d) :" << Max(c,d) << endl; cout << "Max(s,t) :" << Max(s,t) << endl; };
Der Pre-Compiler erstellt die Funktionen
double Max(const double &x,const double &y) { return (x < y ? y:x); } int Max(const int &x,const int &y) { return (x < y ? y:x); } string Max(const string &x,const string &y) { return (x < y ? y:x); }
In obigem Beispiel wurde eine besonders kompakte Variante von Verzweigungen in C/C++ verwendet:
// Kurzform r = (x < y ? y:x); // entspricht if ( x < y ) { r = y; } else { r = x; }