lambda_test.cpp 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. // Boost.Function library
  2. // Copyright Douglas Gregor 2002-2003. Use, modification and
  3. // distribution is subject to the Boost Software License, Version
  4. // 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  5. // http://www.boost.org/LICENSE_1_0.txt)
  6. // For more information, see http://www.boost.org
  7. #include <boost/function.hpp>
  8. #include <boost/lambda/lambda.hpp>
  9. #include <boost/lambda/bind.hpp>
  10. #include <boost/core/lightweight_test.hpp>
  11. #include <iostream>
  12. #include <cstdlib>
  13. static unsigned
  14. func_impl(int arg1, bool arg2, double arg3)
  15. {
  16. using namespace std;
  17. return abs (static_cast<int>((arg2 ? arg1 : 2 * arg1) * arg3));
  18. }
  19. int main()
  20. {
  21. using boost::function;
  22. using namespace boost::lambda;
  23. function <unsigned(bool, double)> f1 = bind(func_impl, 15, _1, _2);
  24. BOOST_TEST_EQ( f1(true, 2.0), 30 );
  25. function <unsigned(double)> f2 = boost::lambda::bind(f1, false, _1);
  26. BOOST_TEST_EQ( f2(2.0), 60 );
  27. function <unsigned()> f3 = boost::lambda::bind(f2, 4.0);
  28. BOOST_TEST_EQ( f3(), 120 );
  29. return boost::report_errors();
  30. }