Statistics
| Branch: | Revision:

root / fiddle / template_function.cpp @ c28093c1

History | View | Annotate | Download (1.09 KB)

1
/*
2
Testing the template function
3
*/
4
#include <iostream>
5
using namespace std;
6

    
7

    
8
template <class T1, class T2>
9
class custom_pair {
10
public:
11
    T1 first;
12
    T2 second;
13

    
14
    custom_pair(const T1& x, const T2& y) : first(x), second(y) // member initialization list
15
    {    }
16

    
17
};
18

    
19
template <class T1, class T2>
20
bool operator<(const custom_pair<T1, T2>& x, const custom_pair<T1, T2>& y)
21
{
22
    return x.first < y.first;
23
}
24

    
25
template <class T1, class T2>
26
std::ostream& operator<< (std::ostream & out, const custom_pair<T1, T2> & data) {
27
    out << "pair: " << data.first << " - " << data.second << endl;
28
    return out ;
29
}
30

    
31
template <class T>
32
const T& custom_max(const T& x, const T& y)
33
{
34
    return x == y ? y : x;
35
}
36

    
37
int main()
38
{
39
    int u = 3, v = 5;
40
    double d = 4.7;
41
    custom_pair<double, long> pair1(3.14, 999);
42
    custom_pair<double, long> pair2(1.6, 999);
43

    
44
    cout << custom_max(u, v) << endl;
45
    cout << custom_max(d, 9.3) << endl;
46
    cout << custom_max(pair1, pair2) << endl;
47
    // cout << custom_max(u, d) << endl; // ERROR: deduced conflicting types for parameter ‘T’ (‘int’ and ‘double’)
48

    
49
}