add.cpp 1.1 KB

1234567891011121314151617181920212223242526272829303132333435
  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/detail/lightweight_test.hpp>
  12. #include <algorithm>
  13. //[add
  14. int main(void) { // Some local scope.
  15. int sum = 0, factor = 10; // Variables in scope to bind.
  16. void BOOST_LOCAL_FUNCTION(const bind factor, bind& sum, int num) {
  17. sum += factor * num;
  18. } BOOST_LOCAL_FUNCTION_NAME(add)
  19. add(1); // Call the local function.
  20. int nums[] = {2, 3};
  21. std::for_each(nums, nums + 2, add); // Pass it to an algorithm.
  22. BOOST_TEST(sum == 60); // Assert final summation value.
  23. return boost::report_errors();
  24. }
  25. //]
  26. #endif // VARIADIC_MACROS