| 1 | // extending_return_type_traits.cpp -- The Boost Lambda Library -------- |
|---|
| 2 | // |
|---|
| 3 | // Copyright (C) 2000-2003 Jaakko Järvi (jaakko.jarvi@cs.utu.fi) |
|---|
| 4 | // Copyright (C) 2000-2003 Gary Powell (powellg@amazon.com) |
|---|
| 5 | // |
|---|
| 6 | // Distributed under the Boost Software License, Version 1.0. (See |
|---|
| 7 | // accompanying file LICENSE_1_0.txt or copy at |
|---|
| 8 | // http://www.boost.org/LICENSE_1_0.txt) |
|---|
| 9 | // |
|---|
| 10 | // For more information, see www.boost.org |
|---|
| 11 | |
|---|
| 12 | // ----------------------------------------------------------------------- |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | #include <boost/test/minimal.hpp> // see "Header Implementation Option" |
|---|
| 16 | |
|---|
| 17 | #include "boost/lambda/bind.hpp" |
|---|
| 18 | #include "boost/lambda/lambda.hpp" |
|---|
| 19 | |
|---|
| 20 | #include <iostream> |
|---|
| 21 | |
|---|
| 22 | #include <functional> |
|---|
| 23 | |
|---|
| 24 | #include <algorithm> |
|---|
| 25 | |
|---|
| 26 | class A {}; |
|---|
| 27 | class B {}; |
|---|
| 28 | |
|---|
| 29 | using namespace boost::lambda; |
|---|
| 30 | |
|---|
| 31 | |
|---|
| 32 | B operator--(const A&, int) { return B(); } |
|---|
| 33 | B operator--(A&) { return B(); } |
|---|
| 34 | B operator++(const A&, int) { return B(); } |
|---|
| 35 | B operator++(A&) { return B(); } |
|---|
| 36 | B operator-(const A&) { return B(); } |
|---|
| 37 | B operator+(const A&) { return B(); } |
|---|
| 38 | |
|---|
| 39 | B operator!(const A&) { return B(); } |
|---|
| 40 | |
|---|
| 41 | B operator&(const A&) { return B(); } |
|---|
| 42 | B operator*(const A&) { return B(); } |
|---|
| 43 | |
|---|
| 44 | namespace boost { |
|---|
| 45 | namespace lambda { |
|---|
| 46 | |
|---|
| 47 | // unary + and - |
|---|
| 48 | template<class Act> |
|---|
| 49 | struct plain_return_type_1<unary_arithmetic_action<Act>, A > { |
|---|
| 50 | typedef B type; |
|---|
| 51 | }; |
|---|
| 52 | |
|---|
| 53 | // post incr/decr |
|---|
| 54 | template<class Act> |
|---|
| 55 | struct plain_return_type_1<post_increment_decrement_action<Act>, A > { |
|---|
| 56 | typedef B type; |
|---|
| 57 | }; |
|---|
| 58 | |
|---|
| 59 | // pre incr/decr |
|---|
| 60 | template<class Act> |
|---|
| 61 | struct plain_return_type_1<pre_increment_decrement_action<Act>, A > { |
|---|
| 62 | typedef B type; |
|---|
| 63 | }; |
|---|
| 64 | // ! |
|---|
| 65 | template<> |
|---|
| 66 | struct plain_return_type_1<logical_action<not_action>, A> { |
|---|
| 67 | typedef B type; |
|---|
| 68 | }; |
|---|
| 69 | // & |
|---|
| 70 | template<> |
|---|
| 71 | struct plain_return_type_1<other_action<addressof_action>, A> { |
|---|
| 72 | typedef B type; |
|---|
| 73 | }; |
|---|
| 74 | // * |
|---|
| 75 | template<> |
|---|
| 76 | struct plain_return_type_1<other_action<contentsof_action>, A> { |
|---|
| 77 | typedef B type; |
|---|
| 78 | }; |
|---|
| 79 | |
|---|
| 80 | |
|---|
| 81 | } // lambda |
|---|
| 82 | } // boost |
|---|
| 83 | |
|---|
| 84 | void ok(B b) {} |
|---|
| 85 | |
|---|
| 86 | void test_unary_operators() |
|---|
| 87 | { |
|---|
| 88 | A a; int i = 1; |
|---|
| 89 | ok((++_1)(a)); |
|---|
| 90 | ok((--_1)(a)); |
|---|
| 91 | ok((_1++)(a)); |
|---|
| 92 | ok((_1--)(a)); |
|---|
| 93 | ok((+_1)(a)); |
|---|
| 94 | ok((-_1)(a)); |
|---|
| 95 | ok((!_1)(a)); |
|---|
| 96 | ok((&_1)(a)); |
|---|
| 97 | ok((*_1)(a)); |
|---|
| 98 | |
|---|
| 99 | BOOST_CHECK((*_1)(make_const(&i)) == 1); |
|---|
| 100 | } |
|---|
| 101 | |
|---|
| 102 | class X {}; |
|---|
| 103 | class Y {}; |
|---|
| 104 | class Z {}; |
|---|
| 105 | |
|---|
| 106 | Z operator+(const X&, const Y&) { return Z(); } |
|---|
| 107 | Z operator-(const X&, const Y&) { return Z(); } |
|---|
| 108 | X operator*(const X&, const Y&) { return X(); } |
|---|
| 109 | |
|---|
| 110 | Z operator/(const X&, const Y&) { return Z(); } |
|---|
| 111 | Z operator%(const X&, const Y&) { return Z(); } |
|---|
| 112 | |
|---|
| 113 | class XX {}; |
|---|
| 114 | class YY {}; |
|---|
| 115 | class ZZ {}; |
|---|
| 116 | class VV {}; |
|---|
| 117 | |
|---|
| 118 | // it is possible to support differently cv-qualified versions |
|---|
| 119 | YY operator*(XX&, YY&) { return YY(); } |
|---|
| 120 | ZZ operator*(const XX&, const YY&) { return ZZ(); } |
|---|
| 121 | XX operator*(volatile XX&, volatile YY&) { return XX(); } |
|---|
| 122 | VV operator*(const volatile XX&, const volatile YY&) { return VV(); } |
|---|
| 123 | |
|---|
| 124 | // the traits can be more complex: |
|---|
| 125 | template <class T> |
|---|
| 126 | class my_vector {}; |
|---|
| 127 | |
|---|
| 128 | template<class A, class B> |
|---|
| 129 | my_vector<typename return_type_2<arithmetic_action<plus_action>, A&, B&>::type> |
|---|
| 130 | operator+(const my_vector<A>& a, const my_vector<B>& b) |
|---|
| 131 | { |
|---|
| 132 | typedef typename |
|---|
| 133 | return_type_2<arithmetic_action<plus_action>, A&, B&>::type res_type; |
|---|
| 134 | return my_vector<res_type>(); |
|---|
| 135 | } |
|---|
| 136 | |
|---|
| 137 | |
|---|
| 138 | |
|---|
| 139 | // bitwise ops: |
|---|
| 140 | X operator<<(const X&, const Y&) { return X(); } |
|---|
| 141 | Z operator>>(const X&, const Y&) { return Z(); } |
|---|
| 142 | Z operator&(const X&, const Y&) { return Z(); } |
|---|
| 143 | Z operator|(const X&, const Y&) { return Z(); } |
|---|
| 144 | Z operator^(const X&, const Y&) { return Z(); } |
|---|
| 145 | |
|---|
| 146 | // comparison ops: |
|---|
| 147 | |
|---|
| 148 | X operator<(const X&, const Y&) { return X(); } |
|---|
| 149 | Z operator>(const X&, const Y&) { return Z(); } |
|---|
| 150 | Z operator<=(const X&, const Y&) { return Z(); } |
|---|
| 151 | Z operator>=(const X&, const Y&) { return Z(); } |
|---|
| 152 | Z operator==(const X&, const Y&) { return Z(); } |
|---|
| 153 | Z operator!=(const X&, const Y&) { return Z(); } |
|---|
| 154 | |
|---|
| 155 | // logical |
|---|
| 156 | |
|---|
| 157 | X operator&&(const X&, const Y&) { return X(); } |
|---|
| 158 | Z operator||(const X&, const Y&) { return Z(); } |
|---|
| 159 | |
|---|
| 160 | // arithh assignment |
|---|
| 161 | |
|---|
| 162 | Z operator+=( X&, const Y&) { return Z(); } |
|---|
| 163 | Z operator-=( X&, const Y&) { return Z(); } |
|---|
| 164 | Y operator*=( X&, const Y&) { return Y(); } |
|---|
| 165 | Z operator/=( X&, const Y&) { return Z(); } |
|---|
| 166 | Z operator%=( X&, const Y&) { return Z(); } |
|---|
| 167 | |
|---|
| 168 | // bitwise assignment |
|---|
| 169 | Z operator<<=( X&, const Y&) { return Z(); } |
|---|
| 170 | Z operator>>=( X&, const Y&) { return Z(); } |
|---|
| 171 | Y operator&=( X&, const Y&) { return Y(); } |
|---|
| 172 | Z operator|=( X&, const Y&) { return Z(); } |
|---|
| 173 | Z operator^=( X&, const Y&) { return Z(); } |
|---|
| 174 | |
|---|
| 175 | // assignment |
|---|
| 176 | class Assign { |
|---|
| 177 | public: |
|---|
| 178 | void operator=(const Assign& a) {} |
|---|
| 179 | X operator[](const int& i) { return X(); } |
|---|
| 180 | }; |
|---|
| 181 | |
|---|
| 182 | |
|---|
| 183 | |
|---|
| 184 | namespace boost { |
|---|
| 185 | namespace lambda { |
|---|
| 186 | |
|---|
| 187 | // you can do action groups |
|---|
| 188 | template<class Act> |
|---|
| 189 | struct plain_return_type_2<arithmetic_action<Act>, X, Y> { |
|---|
| 190 | typedef Z type; |
|---|
| 191 | }; |
|---|
| 192 | |
|---|
| 193 | // or specialize the exact action |
|---|
| 194 | template<> |
|---|
| 195 | struct plain_return_type_2<arithmetic_action<multiply_action>, X, Y> { |
|---|
| 196 | typedef X type; |
|---|
| 197 | }; |
|---|
| 198 | |
|---|
| 199 | // if you want to make a distinction between differently cv-qualified |
|---|
| 200 | // types, you need to specialize on a different level: |
|---|
| 201 | template<> |
|---|
| 202 | struct return_type_2<arithmetic_action<multiply_action>, XX, YY> { |
|---|
| 203 | typedef YY type; |
|---|
| 204 | }; |
|---|
| 205 | template<> |
|---|
| 206 | struct return_type_2<arithmetic_action<multiply_action>, const XX, const YY> { |
|---|
| 207 | typedef ZZ type; |
|---|
| 208 | }; |
|---|
| 209 | template<> |
|---|
| 210 | struct return_type_2<arithmetic_action<multiply_action>, volatile XX, volatile YY> { |
|---|
| 211 | typedef XX type; |
|---|
| 212 | }; |
|---|
| 213 | template<> |
|---|
| 214 | struct return_type_2<arithmetic_action<multiply_action>, volatile const XX, const volatile YY> { |
|---|
| 215 | typedef VV type; |
|---|
| 216 | }; |
|---|
| 217 | |
|---|
| 218 | // the mapping can be more complex: |
|---|
| 219 | template<class A, class B> |
|---|
| 220 | struct plain_return_type_2<arithmetic_action<plus_action>, my_vector<A>, my_vector<B> > { |
|---|
| 221 | typedef typename |
|---|
| 222 | return_type_2<arithmetic_action<plus_action>, A&, B&>::type res_type; |
|---|
| 223 | typedef my_vector<res_type> type; |
|---|
| 224 | }; |
|---|
| 225 | |
|---|
| 226 | // bitwise binary: |
|---|
| 227 | // you can do action groups |
|---|
| 228 | template<class Act> |
|---|
| 229 | struct plain_return_type_2<bitwise_action<Act>, X, Y> { |
|---|
| 230 | typedef Z type; |
|---|
| 231 | }; |
|---|
| 232 | |
|---|
| 233 | // or specialize the exact action |
|---|
| 234 | template<> |
|---|
| 235 | struct plain_return_type_2<bitwise_action<leftshift_action>, X, Y> { |
|---|
| 236 | typedef X type; |
|---|
| 237 | }; |
|---|
| 238 | |
|---|
| 239 | // comparison binary: |
|---|
| 240 | // you can do action groups |
|---|
| 241 | template<class Act> |
|---|
| 242 | struct plain_return_type_2<relational_action<Act>, X, Y> { |
|---|
| 243 | typedef Z type; |
|---|
| 244 | }; |
|---|
| 245 | |
|---|
| 246 | // or specialize the exact action |
|---|
| 247 | template<> |
|---|
| 248 | struct plain_return_type_2<relational_action<less_action>, X, Y> { |
|---|
| 249 | typedef X type; |
|---|
| 250 | }; |
|---|
| 251 | |
|---|
| 252 | // logical binary: |
|---|
| 253 | // you can do action groups |
|---|
| 254 | template<class Act> |
|---|
| 255 | struct plain_return_type_2<logical_action<Act>, X, Y> { |
|---|
| 256 | typedef Z type; |
|---|
| 257 | }; |
|---|
| 258 | |
|---|
| 259 | // or specialize the exact action |
|---|
| 260 | template<> |
|---|
| 261 | struct plain_return_type_2<logical_action<and_action>, X, Y> { |
|---|
| 262 | typedef X type; |
|---|
| 263 | }; |
|---|
| 264 | |
|---|
| 265 | // arithmetic assignment : |
|---|
| 266 | // you can do action groups |
|---|
| 267 | template<class Act> |
|---|
| 268 | struct plain_return_type_2<arithmetic_assignment_action<Act>, X, Y> { |
|---|
| 269 | typedef Z type; |
|---|
| 270 | }; |
|---|
| 271 | |
|---|
| 272 | // or specialize the exact action |
|---|
| 273 | template<> |
|---|
| 274 | struct plain_return_type_2<arithmetic_assignment_action<multiply_action>, X, Y> { |
|---|
| 275 | typedef Y type; |
|---|
| 276 | }; |
|---|
| 277 | |
|---|
| 278 | // arithmetic assignment : |
|---|
| 279 | // you can do action groups |
|---|
| 280 | template<class Act> |
|---|
| 281 | struct plain_return_type_2<bitwise_assignment_action<Act>, X, Y> { |
|---|
| 282 | typedef Z type; |
|---|
| 283 | }; |
|---|
| 284 | |
|---|
| 285 | // or specialize the exact action |
|---|
| 286 | template<> |
|---|
| 287 | struct plain_return_type_2<bitwise_assignment_action<and_action>, X, Y> { |
|---|
| 288 | typedef Y type; |
|---|
| 289 | }; |
|---|
| 290 | |
|---|
| 291 | // assignment |
|---|
| 292 | template<> |
|---|
| 293 | struct plain_return_type_2<other_action<assignment_action>, Assign, Assign> { |
|---|
| 294 | typedef void type; |
|---|
| 295 | }; |
|---|
| 296 | // subscript |
|---|
| 297 | template<> |
|---|
| 298 | struct plain_return_type_2<other_action<subscript_action>, Assign, int> { |
|---|
| 299 | typedef X type; |
|---|
| 300 | }; |
|---|
| 301 | |
|---|
| 302 | |
|---|
| 303 | } // end lambda |
|---|
| 304 | } // end boost |
|---|
| 305 | |
|---|
| 306 | |
|---|
| 307 | |
|---|
| 308 | void test_binary_operators() { |
|---|
| 309 | |
|---|
| 310 | X x; Y y; |
|---|
| 311 | (_1 + _2)(x, y); |
|---|
| 312 | (_1 - _2)(x, y); |
|---|
| 313 | (_1 * _2)(x, y); |
|---|
| 314 | (_1 / _2)(x, y); |
|---|
| 315 | (_1 % _2)(x, y); |
|---|
| 316 | |
|---|
| 317 | |
|---|
| 318 | // make a distinction between differently cv-qualified operators |
|---|
| 319 | XX xx; YY yy; |
|---|
| 320 | const XX& cxx = xx; |
|---|
| 321 | const YY& cyy = yy; |
|---|
| 322 | volatile XX& vxx = xx; |
|---|
| 323 | volatile YY& vyy = yy; |
|---|
| 324 | const volatile XX& cvxx = xx; |
|---|
| 325 | const volatile YY& cvyy = yy; |
|---|
| 326 | |
|---|
| 327 | ZZ dummy1 = (_1 * _2)(cxx, cyy); |
|---|
| 328 | YY dummy2 = (_1 * _2)(xx, yy); |
|---|
| 329 | XX dummy3 = (_1 * _2)(vxx, vyy); |
|---|
| 330 | VV dummy4 = (_1 * _2)(cvxx, cvyy); |
|---|
| 331 | |
|---|
| 332 | my_vector<int> v1; my_vector<double> v2; |
|---|
| 333 | my_vector<double> d = (_1 + _2)(v1, v2); |
|---|
| 334 | |
|---|
| 335 | // bitwise |
|---|
| 336 | |
|---|
| 337 | (_1 << _2)(x, y); |
|---|
| 338 | (_1 >> _2)(x, y); |
|---|
| 339 | (_1 | _2)(x, y); |
|---|
| 340 | (_1 & _2)(x, y); |
|---|
| 341 | (_1 ^ _2)(x, y); |
|---|
| 342 | |
|---|
| 343 | // comparison |
|---|
| 344 | |
|---|
| 345 | (_1 < _2)(x, y); |
|---|
| 346 | (_1 > _2)(x, y); |
|---|
| 347 | (_1 <= _2)(x, y); |
|---|
| 348 | (_1 >= _2)(x, y); |
|---|
| 349 | (_1 == _2)(x, y); |
|---|
| 350 | (_1 != _2)(x, y); |
|---|
| 351 | |
|---|
| 352 | // logical |
|---|
| 353 | |
|---|
| 354 | (_1 || _2)(x, y); |
|---|
| 355 | (_1 && _2)(x, y); |
|---|
| 356 | |
|---|
| 357 | // arithmetic assignment |
|---|
| 358 | (_1 += _2)(x, y); |
|---|
| 359 | (_1 -= _2)(x, y); |
|---|
| 360 | (_1 *= _2)(x, y); |
|---|
| 361 | (_1 /= _2)(x, y); |
|---|
| 362 | (_1 %= _2)(x, y); |
|---|
| 363 | |
|---|
| 364 | // bitwise assignment |
|---|
| 365 | (_1 <<= _2)(x, y); |
|---|
| 366 | (_1 >>= _2)(x, y); |
|---|
| 367 | (_1 |= _2)(x, y); |
|---|
| 368 | (_1 &= _2)(x, y); |
|---|
| 369 | (_1 ^= _2)(x, y); |
|---|
| 370 | |
|---|
| 371 | } |
|---|
| 372 | |
|---|
| 373 | |
|---|
| 374 | int test_main(int, char *[]) { |
|---|
| 375 | test_unary_operators(); |
|---|
| 376 | test_binary_operators(); |
|---|
| 377 | return 0; |
|---|
| 378 | } |
|---|
| 379 | |
|---|
| 380 | |
|---|
| 381 | |
|---|
| 382 | |
|---|
| 383 | |
|---|
| 384 | |
|---|