-
Notifications
You must be signed in to change notification settings - Fork 4
/
strong_types.cpp
116 lines (101 loc) · 3.04 KB
/
strong_types.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// Strong (opaque) types
#include <iostream>
#include <utility>
template <typename T, int N> // N is used for "tagging" the type
struct strong_typedef {
using strong_type = strong_typedef<T, N>; // typedef for the strong type
using type = T; // the wrapped type
T value{}; // the wrapped value, zero by default
strong_typedef() = default;
strong_typedef(const T& x) : value(x) {};
// copy ctors and assignment operators are defaulted
// relational operators
bool operator==(const strong_type& x) const { return value == x.value; }
bool operator<(const strong_type& x) const { return value < x.value; }
// the rest are automatically implemented via
// using namespace std::rel_ops in main()
// Arithmetic operators
strong_type& operator++() {
++value;
return *this;
}
strong_type operator++(int) // postfix
{
strong_type tmp(*this);
++*this;
return tmp;
}
strong_type& operator--() {
--value;
return *this;
}
strong_type operator--(int) // postfix
{
strong_type tmp(*this);
--*this;
return tmp;
}
strong_type& operator+=(const strong_type& rhs) {
value += rhs.value;
return *this;
}
strong_type& operator-=(const strong_type& rhs) {
value -= rhs.value;
return *this;
}
strong_type& operator*=(const strong_type& rhs) {
value *= rhs.value;
return *this;
}
strong_type& operator/=(const strong_type& rhs) {
value /= rhs.value;
return *this;
}
friend std::ostream& operator<<(std::ostream& lhs,
const strong_typedef& rhs) {
lhs << rhs.value;
return lhs;
}
friend std::istream& operator>>(std::istream& lhs, strong_typedef& rhs) {
lhs >> rhs.value;
return lhs;
}
};
template <typename T, int N>
strong_typedef<T, N> operator+(strong_typedef<T, N> lhs,
const strong_typedef<T, N>& rhs) {
lhs += rhs;
return lhs;
}
template <typename T, int N>
strong_typedef<T, N> operator-(strong_typedef<T, N> lhs,
const strong_typedef<T, N>& rhs) {
lhs -= rhs;
return lhs;
}
template <typename T, int N>
strong_typedef<T, N> operator*(strong_typedef<T, N> lhs,
const strong_typedef<T, N>& rhs) {
lhs *= rhs;
return lhs;
}
template <typename T, int N>
strong_typedef<T, N> operator/(strong_typedef<T, N> lhs,
const strong_typedef<T, N>& rhs) {
lhs /= rhs;
return lhs;
}
int main() {
using namespace std::rel_ops;
strong_typedef<double, 0> x, y;
x = 10;
y = 21;
// y = x;
std::cout << std::boolalpha << (y == x) << std::endl;
std::cout << (x / y) << std::endl;
std::cout << (strong_typedef<double, 0>(10) >= x) << std::endl;
strong_typedef<double, 1> z;
std::cin >> z;
std::cout << z << std::endl;
// z + x; // won't compile, type violation
}