To download the code, click here
01: #ifndef DMCGRATH_CLASS_TEMPLATE_EXAMPLE_HPP
02: #define DMCGRATH_CLASS_TEMPLATE_EXAMPLE_HPP
03:
04:
05: namespace dmcgrath{
06: template <typename T1, typename T2>
07: class pair{
08:
09: public:
10: pair() : first_value(T1()), second_value(T2()) {}
11:
12: pair(const T1 &a, const T2 &b) : first_value(a), second_value(b){}
13:
14: //allow for implicit conversion
15: //this will break...horribly...if the types are not implicitly convertible
16: template<typename U, typename V>
17: pair(pair<U, V> &p) : first_value(p.first()), second_value(p.second()){}
18:
19: inline const T1 first(){
20: return first_value;
21: }
22:
23: const T2 second(){
24: return second_value;
25: }
26:
27: //if they are both equal
28: bool operator==(const pair<T1, T2> &RHS){
29: return RHS.first_value == this->first_value && RHS.second_value == this->second_value;
30: }
31:
32: bool operator!=(const pair<T1, T2> &RHS){
33: return !(*this == RHS);
34: }
35:
36: //first takes priority in comparison:
37: bool operator<(const pair<T1, T2> &RHS){
38: return this->first_value < RHS.first_value ||
39: (!(this->first_value < RHS.first_value) && this->second_value < RHS.second_value);
40: }
41:
42: bool operator<=(const pair<T1, T2> &RHS){
43: return (*this == RHS || *this < RHS);
44: }
45:
46: //first takes priority in comparison:
47: bool operator>(const pair<T1, T2> &RHS){
48: return this->first_value > RHS.first_value ||
49: (!(this->first_value > RHS.first_value) && this->second_value > RHS.second_value);
50: }
51:
52:
53:
54: private:
55: T1 first_value;
56: T2 second_value;
57:
58: };
59: }
60:
61: #endif