// result_of_tests.cpp -- The Boost Lambda Library ------------------ // // Copyright (C) 2010 Steven Watanabe // // Distributed under the Boost Software License, Version 1.0. (See // accompanying file LICENSE_1_0.txt or copy at // http://www.boost.org/LICENSE_1_0.txt) // // For more information, see www.boost.org // ----------------------------------------------------------------------- #include #include #include #include #include struct with_result_type { typedef int result_type; int operator()() const { return 0; } int operator()(int) const { return 1; } int operator()(int, int) const { return 2; } int operator()(int, int, int) const { return 3; } int operator()(int, int, int, int) const { return 4; } int operator()(int, int, int, int, int) const { return 5; } int operator()(int, int, int, int, int, int) const { return 6; } int operator()(int, int, int, int, int, int, int) const { return 7; } int operator()(int, int, int, int, int, int, int, int) const { return 8; } int operator()(int, int, int, int, int, int, int, int, int) const { return 9; } }; struct with_result_template_value { template struct result; template struct result { typedef int type; }; template struct result { BOOST_MPL_ASSERT((boost::is_same)); typedef int type; }; template struct result { BOOST_MPL_ASSERT((boost::is_same)); BOOST_MPL_ASSERT((boost::is_same)); typedef int type; }; template struct result { BOOST_MPL_ASSERT((boost::is_same)); BOOST_MPL_ASSERT((boost::is_same)); BOOST_MPL_ASSERT((boost::is_same)); typedef int type; }; template struct result { BOOST_MPL_ASSERT((boost::is_same)); BOOST_MPL_ASSERT((boost::is_same)); BOOST_MPL_ASSERT((boost::is_same)); BOOST_MPL_ASSERT((boost::is_same)); typedef int type; }; template struct result { BOOST_MPL_ASSERT((boost::is_same)); BOOST_MPL_ASSERT((boost::is_same)); BOOST_MPL_ASSERT((boost::is_same)); BOOST_MPL_ASSERT((boost::is_same)); BOOST_MPL_ASSERT((boost::is_same)); typedef int type; }; template struct result { BOOST_MPL_ASSERT((boost::is_same)); BOOST_MPL_ASSERT((boost::is_same)); BOOST_MPL_ASSERT((boost::is_same)); BOOST_MPL_ASSERT((boost::is_same)); BOOST_MPL_ASSERT((boost::is_same)); BOOST_MPL_ASSERT((boost::is_same)); typedef int type; }; template struct result { BOOST_MPL_ASSERT((boost::is_same)); BOOST_MPL_ASSERT((boost::is_same)); BOOST_MPL_ASSERT((boost::is_same)); BOOST_MPL_ASSERT((boost::is_same)); BOOST_MPL_ASSERT((boost::is_same)); BOOST_MPL_ASSERT((boost::is_same)); BOOST_MPL_ASSERT((boost::is_same)); typedef int type; }; template struct result { BOOST_MPL_ASSERT((boost::is_same)); BOOST_MPL_ASSERT((boost::is_same)); BOOST_MPL_ASSERT((boost::is_same)); BOOST_MPL_ASSERT((boost::is_same)); BOOST_MPL_ASSERT((boost::is_same)); BOOST_MPL_ASSERT((boost::is_same)); BOOST_MPL_ASSERT((boost::is_same)); BOOST_MPL_ASSERT((boost::is_same)); typedef int type; }; template struct result { BOOST_MPL_ASSERT((boost::is_same)); BOOST_MPL_ASSERT((boost::is_same)); BOOST_MPL_ASSERT((boost::is_same)); BOOST_MPL_ASSERT((boost::is_same)); BOOST_MPL_ASSERT((boost::is_same)); BOOST_MPL_ASSERT((boost::is_same)); BOOST_MPL_ASSERT((boost::is_same)); BOOST_MPL_ASSERT((boost::is_same)); BOOST_MPL_ASSERT((boost::is_same)); typedef int type; }; int operator()() const { return 0; } int operator()(int) const { return 1; } int operator()(int, int) const { return 2; } int operator()(int, int, int) const { return 3; } int operator()(int, int, int, int) const { return 4; } int operator()(int, int, int, int, int) const { return 5; } int operator()(int, int, int, int, int, int) const { return 6; } int operator()(int, int, int, int, int, int, int) const { return 7; } int operator()(int, int, int, int, int, int, int, int) const { return 8; } int operator()(int, int, int, int, int, int, int, int, int) const { return 9; } }; struct with_result_template_reference { template struct result; template struct result { typedef int type; }; template struct result { BOOST_MPL_ASSERT((boost::is_same)); typedef int type; }; template struct result { BOOST_MPL_ASSERT((boost::is_same)); BOOST_MPL_ASSERT((boost::is_same)); typedef int type; }; template struct result { BOOST_MPL_ASSERT((boost::is_same)); BOOST_MPL_ASSERT((boost::is_same)); BOOST_MPL_ASSERT((boost::is_same)); typedef int type; }; template struct result { BOOST_MPL_ASSERT((boost::is_same)); BOOST_MPL_ASSERT((boost::is_same)); BOOST_MPL_ASSERT((boost::is_same)); BOOST_MPL_ASSERT((boost::is_same)); typedef int type; }; template struct result { BOOST_MPL_ASSERT((boost::is_same)); BOOST_MPL_ASSERT((boost::is_same)); BOOST_MPL_ASSERT((boost::is_same)); BOOST_MPL_ASSERT((boost::is_same)); BOOST_MPL_ASSERT((boost::is_same)); typedef int type; }; template struct result { BOOST_MPL_ASSERT((boost::is_same)); BOOST_MPL_ASSERT((boost::is_same)); BOOST_MPL_ASSERT((boost::is_same)); BOOST_MPL_ASSERT((boost::is_same)); BOOST_MPL_ASSERT((boost::is_same)); BOOST_MPL_ASSERT((boost::is_same)); typedef int type; }; template struct result { BOOST_MPL_ASSERT((boost::is_same)); BOOST_MPL_ASSERT((boost::is_same)); BOOST_MPL_ASSERT((boost::is_same)); BOOST_MPL_ASSERT((boost::is_same)); BOOST_MPL_ASSERT((boost::is_same)); BOOST_MPL_ASSERT((boost::is_same)); BOOST_MPL_ASSERT((boost::is_same)); typedef int type; }; template struct result { BOOST_MPL_ASSERT((boost::is_same)); BOOST_MPL_ASSERT((boost::is_same)); BOOST_MPL_ASSERT((boost::is_same)); BOOST_MPL_ASSERT((boost::is_same)); BOOST_MPL_ASSERT((boost::is_same)); BOOST_MPL_ASSERT((boost::is_same)); BOOST_MPL_ASSERT((boost::is_same)); BOOST_MPL_ASSERT((boost::is_same)); typedef int type; }; template struct result { BOOST_MPL_ASSERT((boost::is_same)); BOOST_MPL_ASSERT((boost::is_same)); BOOST_MPL_ASSERT((boost::is_same)); BOOST_MPL_ASSERT((boost::is_same)); BOOST_MPL_ASSERT((boost::is_same)); BOOST_MPL_ASSERT((boost::is_same)); BOOST_MPL_ASSERT((boost::is_same)); BOOST_MPL_ASSERT((boost::is_same)); BOOST_MPL_ASSERT((boost::is_same)); typedef int type; }; int operator()() const { return 0; } int operator()(int) const { return 1; } int operator()(int, int) const { return 2; } int operator()(int, int, int) const { return 3; } int operator()(int, int, int, int) const { return 4; } int operator()(int, int, int, int, int) const { return 5; } int operator()(int, int, int, int, int, int) const { return 6; } int operator()(int, int, int, int, int, int, int) const { return 7; } int operator()(int, int, int, int, int, int, int, int) const { return 8; } int operator()(int, int, int, int, int, int, int, int, int) const { return 9; } }; template typename boost::result_of::type apply0(F f) { return f(); } template typename boost::result_of::type apply1(F f, A a) { return f(a); } template typename boost::result_of::type apply2(F f, A a, B b) { return f(a, b); } template typename boost::result_of::type apply3(F f, A a, B b, C c) { return f(a, b, c); } using namespace boost::lambda; int main() { BOOST_TEST_EQ(boost::lambda::bind(with_result_type())(), 0); BOOST_TEST_EQ(boost::lambda::bind(with_result_type(), 1)(), 1); BOOST_TEST_EQ(boost::lambda::bind(with_result_type(), 1, 2)(), 2); BOOST_TEST_EQ(boost::lambda::bind(with_result_type(), 1, 2, 3)(), 3); BOOST_TEST_EQ(boost::lambda::bind(with_result_type(), 1, 2, 3, 4)(), 4); BOOST_TEST_EQ(boost::lambda::bind(with_result_type(), 1, 2, 3, 4, 5)(), 5); BOOST_TEST_EQ(boost::lambda::bind(with_result_type(), 1, 2, 3, 4, 5, 6)(), 6); BOOST_TEST_EQ(boost::lambda::bind(with_result_type(), 1, 2, 3, 4, 5, 6, 7)(), 7); BOOST_TEST_EQ(boost::lambda::bind(with_result_type(), 1, 2, 3, 4, 5, 6, 7, 8)(), 8); BOOST_TEST_EQ(boost::lambda::bind(with_result_type(), 1, 2, 3, 4, 5, 6, 7, 8, 9)(), 9); // Nullary result_of fails //BOOST_TEST_EQ(boost::lambda::bind(with_result_template_value())(), 0); BOOST_TEST_EQ(boost::lambda::bind(with_result_template_value(), 1)(), 1); BOOST_TEST_EQ(boost::lambda::bind(with_result_template_value(), 1, 2)(), 2); BOOST_TEST_EQ(boost::lambda::bind(with_result_template_value(), 1, 2, 3)(), 3); BOOST_TEST_EQ(boost::lambda::bind(with_result_template_value(), 1, 2, 3, 4)(), 4); BOOST_TEST_EQ(boost::lambda::bind(with_result_template_value(), 1, 2, 3, 4, 5)(), 5); BOOST_TEST_EQ(boost::lambda::bind(with_result_template_value(), 1, 2, 3, 4, 5, 6)(), 6); BOOST_TEST_EQ(boost::lambda::bind(with_result_template_value(), 1, 2, 3, 4, 5, 6, 7)(), 7); BOOST_TEST_EQ(boost::lambda::bind(with_result_template_value(), 1, 2, 3, 4, 5, 6, 7, 8)(), 8); BOOST_TEST_EQ(boost::lambda::bind(with_result_template_value(), 1, 2, 3, 4, 5, 6, 7, 8, 9)(), 9); int one = 1, two = 2, three = 3, four = 4, five = 5, six = 6, seven = 7, eight = 8, nine = 9; // Nullary result_of fails //BOOST_TEST_EQ(boost::lambda::bind(with_result_template_reference())(), 0); BOOST_TEST_EQ(boost::lambda::bind(with_result_template_reference(), var(one))(), 1); BOOST_TEST_EQ(boost::lambda::bind(with_result_template_reference(), var(one), var(two))(), 2); BOOST_TEST_EQ(boost::lambda::bind(with_result_template_reference(), var(one), var(two), var(three))(), 3); BOOST_TEST_EQ(boost::lambda::bind(with_result_template_reference(), var(one), var(two), var(three), var(four))(), 4); BOOST_TEST_EQ(boost::lambda::bind(with_result_template_reference(), var(one), var(two), var(three), var(four), var(five))(), 5); BOOST_TEST_EQ(boost::lambda::bind(with_result_template_reference(), var(one), var(two), var(three), var(four), var(five), var(six))(), 6); BOOST_TEST_EQ(boost::lambda::bind(with_result_template_reference(), var(one), var(two), var(three), var(four), var(five), var(six), var(seven))(), 7); BOOST_TEST_EQ(boost::lambda::bind(with_result_template_reference(), var(one), var(two), var(three), var(four), var(five), var(six), var(seven), var(eight))(), 8); BOOST_TEST_EQ(boost::lambda::bind(with_result_template_reference(), var(one), var(two), var(three), var(four), var(five), var(six), var(seven), var(eight), var(nine))(), 9); // Check using result_of with lambda functors //BOOST_TEST_EQ(apply0(constant(0)), 0); BOOST_TEST_EQ(apply1(_1, one), 1); BOOST_TEST_EQ(apply1(_1, one), 1); BOOST_TEST_EQ(apply1(_1, one), 1); BOOST_TEST_EQ((apply2(_1 + _2, one, two)), 3); BOOST_TEST_EQ((apply2(_1 + _2, one, two)), 3); BOOST_TEST_EQ((apply2(_1 + _2, one, two)), 3); BOOST_TEST_EQ((apply3(_1 + _2 + _3, one, two, three)), 6); BOOST_TEST_EQ((apply3(_1 + _2 + _3, one, two, three)), 6); BOOST_TEST_EQ((apply3(_1 + _2 + _3, one, two, three)), 6); return boost::report_errors(); }