PracticeDev/study_cpp/cpp_primer_source_code/Chapter 8/choices.cpp

32 lines
662 B
C++
Raw Permalink Normal View History

2022-12-20 17:31:11 +08:00
// choices.cpp -- choosing a template
#include <iostream>
template<class T>
T lesser(T a, T b) // #1
{
return a < b ? a : b;
}
int lesser (int a, int b) // #2
{
a = a < 0 ? -a : a;
b = b < 0 ? -b : b;
return a < b ? a : b;
}
int main()
{
using namespace std;
int m = 20;
int n = -30;
double x = 15.5;
double y = 25.9;
cout << lesser(m, n) << endl; // use #2
cout << lesser(x, y) << endl; // use #1 with double
cout << lesser<>(m, n) << endl; // use #1 with int
cout << lesser<int>(x, y) << endl; // use #1 with int
// cin.get();
return 0;
}