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