add_cxx11_lambda.cpp 983 B

12345678910111213141516171819202122232425262728293031323334
  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_LAMBDAS
  8. # error "lambda functions required"
  9. #else
  10. #include <boost/detail/lightweight_test.hpp>
  11. #include <algorithm>
  12. //[add_cxx11_lambda
  13. int main(void) { // Some local scope.
  14. int sum = 0, factor = 10; // Variables in scope to bind.
  15. auto add = [factor, &sum](int num) { // C++11 only.
  16. sum += factor * num;
  17. };
  18. add(1); // Call the lambda.
  19. int nums[] = {2, 3};
  20. std::for_each(nums, nums + 2, add); // Pass it to an algorithm.
  21. BOOST_TEST(sum == 60); // Assert final summation value.
  22. return boost::report_errors();
  23. }
  24. //]
  25. #endif