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_VARIADIC_MACROS 10 # error "variadic macros required" 11 #else 12 13 #include <boost/local_function.hpp> 14 #include <boost/detail/lightweight_test.hpp> 15 #include <vector> 16 #include <algorithm> 17 18 //[add_typed 19 struct adder { adderadder20 adder(void) : sum_(0) {} 21 sumadder22 int sum(const std::vector<int>& nums, const int& factor = 10) { 23 // Explicitly specify bound variable and return types (no type-of). 24 BOOST_LOCAL_FUNCTION(const bind(const int&) factor, 25 bind(adder*) this_, int num, return int) { 26 return this_->sum_ += factor * num; 27 } BOOST_LOCAL_FUNCTION_NAME(add) 28 29 std::for_each(nums.begin(), nums.end(), add); 30 return sum_; 31 } 32 33 private: 34 int sum_; 35 }; 36 //] 37 main(void)38int main(void) { 39 std::vector<int> v(3); 40 v[0] = 1; v[1] = 2; v[2] = 3; 41 42 BOOST_TEST(adder().sum(v) == 60); 43 return boost::report_errors(); 44 } 45 46 #endif // VARIADIC_MACROS 47 48