return_assign.cpp 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. // Copyright (C) 2009-2012 Lorenzo Caminiti
  2. // Distributed under the Boost Software License, Version 1.0
  3. // (see accompanying file LICENSE_1_0.txt or a copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. // Home at http://www.boost.org/libs/local_function
  6. #include <boost/config.hpp>
  7. #ifdef BOOST_NO_CXX11_VARIADIC_MACROS
  8. # error "variadic macros required"
  9. #else
  10. #include <boost/local_function.hpp>
  11. #include <boost/function.hpp>
  12. #include <boost/detail/lightweight_test.hpp>
  13. #include <iostream>
  14. //[return_assign
  15. void call1(boost::function<int (int) > f) { BOOST_TEST(f(1) == 5); }
  16. void call0(boost::function<int (void)> f) { BOOST_TEST(f() == 5); }
  17. boost::function<int (int, int)> linear(const int& slope) {
  18. int BOOST_LOCAL_FUNCTION(const bind& slope,
  19. int x, default 1, int y, default 2) {
  20. return x + slope * y;
  21. } BOOST_LOCAL_FUNCTION_NAME(lin)
  22. boost::function<int (int, int)> f = lin; // Assign to local variable.
  23. BOOST_TEST(f(1, 2) == 5);
  24. call1(lin); // Pass to other functions.
  25. call0(lin);
  26. return lin; // Return.
  27. }
  28. void call(void) {
  29. boost::function<int (int, int)> f = linear(2);
  30. BOOST_TEST(f(1, 2) == 5);
  31. }
  32. //]
  33. int main(void) {
  34. call();
  35. return boost::report_errors();
  36. }
  37. #endif // VARIADIC_MACROS