1 2 // Copyright (C) 2009-2012 Lorenzo Caminiti 3 // Distributed under the Boost Software License, Version 1.0 4 // (see accompanying file LICENSE_1_0.txt or a copy at 5 // http://www.boost.org/LICENSE_1_0.txt) 6 // Home at http://www.boost.org/libs/local_function 7 8 #include <boost/config.hpp> 9 #ifdef BOOST_NO_CXX11_LAMBDAS 10 # error "lambda functions required" 11 #else 12 13 #include <boost/detail/lightweight_test.hpp> 14 #include <algorithm> 15 16 //[add_cxx11_lambda main(void)17int main(void) { // Some local scope. 18 int sum = 0, factor = 10; // Variables in scope to bind. 19 20 auto add = [factor, &sum](int num) { // C++11 only. 21 sum += factor * num; 22 }; 23 24 add(1); // Call the lambda. 25 int nums[] = {2, 3}; 26 std::for_each(nums, nums + 2, add); // Pass it to an algorithm. 27 28 BOOST_TEST(sum == 60); // Assert final summation value. 29 return boost::report_errors(); 30 } 31 //] 32 33 #endif 34 35