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/detail/lightweight_test.hpp> 9 10 //[add_local_functor main(void)11int main(void) { 12 int sum = 0, factor = 10; 13 14 struct local_add { // Unfortunately, boilerplate code to program the class. 15 local_add(int& _sum, int _factor): sum(_sum), factor(_factor) {} 16 17 inline void operator()(int num) { // Body uses C++ statement syntax. 18 sum += factor * num; 19 } 20 21 private: // Unfortunately, cannot bind so repeat variable types. 22 int& sum; // Access `sum` by reference. 23 const int factor; // Make `factor` constant. 24 } add(sum, factor); 25 26 add(1); 27 int nums[] = {2, 3}; 28 // Unfortunately, cannot pass as template parameter to `std::for_each`. 29 for(size_t i = 0; i < 2; ++i) add(nums[i]); 30 31 BOOST_TEST(sum == 60); 32 return boost::report_errors(); 33 } 34 //] 35 36