| 1 | // ret_test.cpp - The Boost Lambda Library ----------------------- |
| 2 | // |
| 3 | // Copyright (C) 2009 Steven Watanabe |
| 4 | // |
| 5 | // Distributed under the Boost Software License, Version 1.0. (See |
| 6 | // accompanying file LICENSE_1_0.txt or copy at |
| 7 | // http://www.boost.org/LICENSE_1_0.txt) |
| 8 | // |
| 9 | // For more information, see www.boost.org |
| 10 | |
| 11 | #include <boost/core/lightweight_test.hpp> |
| 12 | |
| 13 | #include <boost/lambda/lambda.hpp> |
| 14 | |
| 15 | #include <boost/mpl/assert.hpp> |
| 16 | #include <boost/type_traits/is_same.hpp> |
| 17 | |
| 18 | template<class R, class F> |
| 19 | void test_ret(R r, F f) { |
| 20 | typename F::result_type x = f(); |
| 21 | BOOST_MPL_ASSERT((boost::is_same<R, typename F::result_type>)); |
| 22 | BOOST_TEST(x == r); |
| 23 | } |
| 24 | |
| 25 | template<class R, class F, class T1> |
| 26 | void test_ret(R r, F f, T1& t1) { |
| 27 | typename F::result_type x = f(t1); |
| 28 | BOOST_MPL_ASSERT((boost::is_same<R, typename F::result_type>)); |
| 29 | BOOST_TEST(x == r); |
| 30 | } |
| 31 | |
| 32 | class add_result { |
| 33 | public: |
| 34 | add_result(int i = 0) : value(i) {} |
| 35 | friend bool operator==(const add_result& lhs, const add_result& rhs) { |
| 36 | return(lhs.value == rhs.value); |
| 37 | } |
| 38 | private: |
| 39 | int value; |
| 40 | }; |
| 41 | |
| 42 | class addable {}; |
| 43 | add_result operator+(addable, addable) { |
| 44 | return add_result(7); |
| 45 | } |
| 46 | |
| 47 | int main() |
| 48 | { |
| 49 | addable test; |
| 50 | test_ret(r: add_result(7), f: boost::lambda::ret<add_result>(a1: boost::lambda::_1 + test), t1&: test); |
| 51 | test_ret(r: 8.0, f: boost::lambda::ret<double>(a1: boost::lambda::constant(t: 7) + 1)); |
| 52 | |
| 53 | return boost::report_errors(); |
| 54 | } |
| 55 | |