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/local_function.hpp>
9 #include <boost/typeof/typeof.hpp>
10 #include BOOST_TYPEOF_INCREMENT_REGISTRATION_GROUP()
11 #include <boost/detail/lightweight_test.hpp>
12 #include <vector>
13 #include <algorithm>
14 
15 struct adder;
16 BOOST_TYPEOF_REGISTER_TYPE(adder) // Register before `bind this_` below.
17 
18 struct adder {
adderadder19     adder() : sum_(0) {}
20 
sumadder21     int sum(const std::vector<int>& nums, const int factor = 10) {
22 
23         void BOOST_LOCAL_FUNCTION( (const bind factor) (bind this_)
24                 (int num) ) {
25             this_->sum_ += factor * num;
26         } BOOST_LOCAL_FUNCTION_NAME(add)
27 
28         std::for_each(nums.begin(), nums.end(), add);
29         return sum_;
30     }
31 
32 private:
33     int sum_;
34 };
35 
main(void)36 int main(void) {
37     std::vector<int> v(3);
38     v[0] = 1; v[1] = 2; v[2] = 3;
39 
40     BOOST_TEST(adder().sum(v) == 60);
41     return boost::report_errors();
42 }
43 
44