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
15struct adder;
16BOOST_TYPEOF_REGISTER_TYPE(adder) // Register before `bind this_` below.
17
18struct adder {
19 adder() : sum_(0) {}
20
21 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(first: nums.begin(), last: nums.end(), f: add);
29 return sum_;
30 }
31
32private:
33 int sum_;
34};
35
36int 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

source code of boost/libs/local_function/test/add_this_seq.cpp