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