|
1 | 1 | // The Art of C++ / Operators |
2 | | -// Copyright (c) 2013-2017 Daniel Frey |
| 2 | +// Copyright (c) 2013-2018 Daniel Frey |
3 | 3 | // Please see LICENSE for license or visit https://github.com/taocpp/operators/ |
4 | 4 |
|
5 | 5 | #include <tao/operators.hpp> |
|
8 | 8 | #include <type_traits> |
9 | 9 |
|
10 | 10 | class X |
11 | | - : tao::operators::ordered_field< X >, |
12 | | - tao::operators::modable< X >, |
13 | | - tao::operators::ordered_field< X, int >, |
14 | | - tao::operators::modable< X, int > |
| 11 | + : tao::operators::ordered_field< X >, |
| 12 | + tao::operators::modable< X >, |
| 13 | + tao::operators::ordered_field< X, int >, |
| 14 | + tao::operators::modable< X, int > |
15 | 15 | { |
16 | 16 | public: |
17 | | - explicit X( const int v ) noexcept : v_( v ) {} |
18 | | - |
19 | | - X( const X& x ) noexcept : v_( x.v_ ) {} |
| 17 | + explicit X( const int v ) noexcept |
| 18 | + : v_( v ) |
| 19 | + { |
| 20 | + } |
| 21 | + |
| 22 | + X( const X& x ) |
| 23 | + noexcept |
| 24 | + : v_( x.v_ ) |
| 25 | + { |
| 26 | + } |
| 27 | + |
| 28 | + X& operator+=( const X& x ) noexcept |
| 29 | + { |
| 30 | + v_ += x.v_; |
| 31 | + return *this; |
| 32 | + } |
| 33 | + |
| 34 | + X& operator-=( const X& x ) |
| 35 | + { |
| 36 | + v_ -= x.v_; |
| 37 | + return *this; |
| 38 | + } |
| 39 | + |
| 40 | + X& operator*=( const X& x ) |
| 41 | + { |
| 42 | + v_ *= x.v_; |
| 43 | + return *this; |
| 44 | + } |
| 45 | + |
| 46 | + X& operator/=( const X& x ) |
| 47 | + { |
| 48 | + v_ /= x.v_; |
| 49 | + return *this; |
| 50 | + } |
| 51 | + |
| 52 | + X& operator%=( const X& x ) |
| 53 | + { |
| 54 | + v_ %= x.v_; |
| 55 | + return *this; |
| 56 | + } |
| 57 | + |
| 58 | + X& operator+=( const int v ) |
| 59 | + { |
| 60 | + v_ += v; |
| 61 | + return *this; |
| 62 | + } |
| 63 | + |
| 64 | + X& operator-=( const int v ) |
| 65 | + { |
| 66 | + v_ -= v; |
| 67 | + return *this; |
| 68 | + } |
| 69 | + |
| 70 | + X& operator*=( const int v ) |
| 71 | + { |
| 72 | + v_ *= v; |
| 73 | + return *this; |
| 74 | + } |
| 75 | + |
| 76 | + X& operator/=( const int v ) |
| 77 | + { |
| 78 | + v_ /= v; |
| 79 | + return *this; |
| 80 | + } |
| 81 | + |
| 82 | + X& operator%=( const int v ) |
| 83 | + { |
| 84 | + v_ %= v; |
| 85 | + return *this; |
| 86 | + } |
| 87 | + |
| 88 | + int v_; |
| 89 | +}; |
20 | 90 |
|
21 | | - X& operator+=( const X& x ) noexcept { v_ += x.v_; return *this; } |
22 | | - X& operator-=( const X& x ) { v_ -= x.v_; return *this; } |
23 | | - X& operator*=( const X& x ) { v_ *= x.v_; return *this; } |
24 | | - X& operator/=( const X& x ) { v_ /= x.v_; return *this; } |
25 | | - X& operator%=( const X& x ) { v_ %= x.v_; return *this; } |
| 91 | +bool operator==( const X& lhs, const X& rhs ) |
| 92 | +{ |
| 93 | + return lhs.v_ == rhs.v_; |
| 94 | +} |
26 | 95 |
|
27 | | - X& operator+=( const int v ) { v_ += v; return *this; } |
28 | | - X& operator-=( const int v ) { v_ -= v; return *this; } |
29 | | - X& operator*=( const int v ) { v_ *= v; return *this; } |
30 | | - X& operator/=( const int v ) { v_ /= v; return *this; } |
31 | | - X& operator%=( const int v ) { v_ %= v; return *this; } |
| 96 | +bool operator<( const X& lhs, const X& rhs ) |
| 97 | +{ |
| 98 | + return lhs.v_ < rhs.v_; |
| 99 | +} |
32 | 100 |
|
33 | | - int v_; |
34 | | -}; |
| 101 | +bool operator==( const X& x, const int v ) |
| 102 | +{ |
| 103 | + return x.v_ == v; |
| 104 | +} |
35 | 105 |
|
36 | | -bool operator==( const X& lhs, const X& rhs ) { return lhs.v_ == rhs.v_; } |
37 | | -bool operator<( const X& lhs, const X& rhs ) { return lhs.v_ < rhs.v_; } |
| 106 | +bool operator<( const X& x, const int v ) |
| 107 | +{ |
| 108 | + return x.v_ < v; |
| 109 | +} |
38 | 110 |
|
39 | | -bool operator==( const X& x, const int v ) { return x.v_ == v; } |
40 | | -bool operator<( const X& x, const int v ) { return x.v_ < v; } |
41 | | -bool operator>( const X& x, const int v ) { return x.v_ > v; } |
| 111 | +bool operator>( const X& x, const int v ) |
| 112 | +{ |
| 113 | + return x.v_ > v; |
| 114 | +} |
42 | 115 |
|
43 | | -class E : tao::operators::ordered_field< E > {}; |
| 116 | +class E |
| 117 | + : tao::operators::ordered_field< E > |
| 118 | +{ |
| 119 | +}; |
44 | 120 |
|
45 | 121 | void adl_test( const E& ) {} |
46 | 122 |
|
47 | 123 | namespace tao |
48 | 124 | { |
49 | | - void adl_test( const E& ); |
| 125 | + void adl_test( const E& ); |
50 | 126 | } |
51 | 127 |
|
52 | | -struct S : tao::operators::addable< S > |
| 128 | +struct S |
| 129 | + : tao::operators::addable< S > |
53 | 130 | { |
54 | | - S() {} |
55 | | - S( const S& a, const S& b ) : S( a + b ) {} |
56 | | - S& operator+=( const S& ) noexcept { return *this; } |
| 131 | + S() {} |
| 132 | + |
| 133 | + S( const S& a, const S& b ) |
| 134 | + : S( a + b ) |
| 135 | + { |
| 136 | + } |
| 137 | + |
| 138 | + S& operator+=( const S& ) noexcept |
| 139 | + { |
| 140 | + return *this; |
| 141 | + } |
57 | 142 | }; |
58 | 143 |
|
59 | 144 | int main() |
60 | 145 | { |
61 | | - X x1( 1 ); |
62 | | - X x2( 2 ); |
63 | | - X x3( 3 ); |
64 | | - |
65 | | - static_assert( noexcept( x1 + x2 ), "oops" ); |
66 | | - static_assert( !noexcept( x1 * x2 ), "oops" ); |
67 | | - |
68 | | - assert( x1 == x1 ); |
69 | | - assert( x1 != x2 ); |
70 | | - |
71 | | - assert( x1 == 1 ); |
72 | | - assert( 2 == x2 ); |
73 | | - assert( x3 != 1 ); |
74 | | - assert( 2 != x3 ); |
75 | | - |
76 | | - assert( x1 < x2 ); |
77 | | - assert( x1 <= x2 ); |
78 | | - assert( x2 <= x2 ); |
79 | | - assert( x3 > x2 ); |
80 | | - assert( x3 >= x2 ); |
81 | | - assert( x2 >= x2 ); |
82 | | - |
83 | | - assert( x1 < 2 ); |
84 | | - assert( x1 <= 2 ); |
85 | | - assert( x2 <= 2 ); |
86 | | - assert( x3 > 2 ); |
87 | | - assert( x3 >= 2 ); |
88 | | - assert( x2 >= 2 ); |
89 | | - |
90 | | - assert( 1 < x2 ); |
91 | | - assert( 1 <= x2 ); |
92 | | - assert( 2 <= x2 ); |
93 | | - assert( 3 > x2 ); |
94 | | - assert( 3 >= x2 ); |
95 | | - assert( 2 >= x2 ); |
96 | | - |
97 | | - assert( x1 + x2 == x3 ); |
98 | | - assert( 1 + x2 == x3 ); |
99 | | - assert( x1 + 2 == x3 ); |
100 | | - assert( x2 + x1 == 3 ); |
101 | | - |
102 | | - assert( x3 - x1 == x2 ); |
103 | | - assert( 3 - x1 == x2 ); |
104 | | - assert( x3 - 1 == x2 ); |
105 | | - assert( x1 - x3 == -2 ); |
106 | | - |
107 | | - assert( x2 * x2 == 4 ); |
108 | | - assert( x2 * 3 == 6 ); |
109 | | - assert( 4 * x2 == 8 ); |
110 | | - |
111 | | - assert( ( x3 + x1 ) / x2 == 2 ); |
112 | | - assert( ( x1 + x3 ) / 2 == x2 ); |
113 | | - |
114 | | - assert( x3 % x2 == 1 ); |
115 | | - assert( x3 % 2 == 1 ); |
| 146 | + X x1( 1 ); |
| 147 | + X x2( 2 ); |
| 148 | + X x3( 3 ); |
| 149 | + |
| 150 | + static_assert( noexcept( x1 + x2 ), "oops" ); |
| 151 | + static_assert( !noexcept( x1 * x2 ), "oops" ); |
| 152 | + |
| 153 | + assert( x1 == x1 ); |
| 154 | + assert( x1 != x2 ); |
| 155 | + |
| 156 | + assert( x1 == 1 ); |
| 157 | + assert( 2 == x2 ); |
| 158 | + assert( x3 != 1 ); |
| 159 | + assert( 2 != x3 ); |
| 160 | + |
| 161 | + assert( x1 < x2 ); |
| 162 | + assert( x1 <= x2 ); |
| 163 | + assert( x2 <= x2 ); |
| 164 | + assert( x3 > x2 ); |
| 165 | + assert( x3 >= x2 ); |
| 166 | + assert( x2 >= x2 ); |
| 167 | + |
| 168 | + assert( x1 < 2 ); |
| 169 | + assert( x1 <= 2 ); |
| 170 | + assert( x2 <= 2 ); |
| 171 | + assert( x3 > 2 ); |
| 172 | + assert( x3 >= 2 ); |
| 173 | + assert( x2 >= 2 ); |
| 174 | + |
| 175 | + assert( 1 < x2 ); |
| 176 | + assert( 1 <= x2 ); |
| 177 | + assert( 2 <= x2 ); |
| 178 | + assert( 3 > x2 ); |
| 179 | + assert( 3 >= x2 ); |
| 180 | + assert( 2 >= x2 ); |
| 181 | + |
| 182 | + assert( x1 + x2 == x3 ); |
| 183 | + assert( 1 + x2 == x3 ); |
| 184 | + assert( x1 + 2 == x3 ); |
| 185 | + assert( x2 + x1 == 3 ); |
| 186 | + |
| 187 | + assert( x3 - x1 == x2 ); |
| 188 | + assert( 3 - x1 == x2 ); |
| 189 | + assert( x3 - 1 == x2 ); |
| 190 | + assert( x1 - x3 == -2 ); |
| 191 | + |
| 192 | + assert( x2 * x2 == 4 ); |
| 193 | + assert( x2 * 3 == 6 ); |
| 194 | + assert( 4 * x2 == 8 ); |
| 195 | + |
| 196 | + assert( ( x3 + x1 ) / x2 == 2 ); |
| 197 | + assert( ( x1 + x3 ) / 2 == x2 ); |
| 198 | + |
| 199 | + assert( x3 % x2 == 1 ); |
| 200 | + assert( x3 % 2 == 1 ); |
116 | 201 |
|
117 | 202 | #ifndef _WIN32 |
118 | | - static_assert( std::is_empty< E >::value, "oops" ); |
| 203 | + static_assert( std::is_empty< E >::value, "oops" ); |
119 | 204 | #endif |
120 | 205 |
|
121 | | - adl_test( E() ); |
| 206 | + adl_test( E{} ); |
122 | 207 |
|
123 | | - S s; |
124 | | - S s2( s, s ); |
| 208 | + S s; |
| 209 | + S s2( s, s ); |
125 | 210 | } |
0 commit comments