• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 v;
16 BOOST_TYPEOF_REGISTER_TYPE(v) // Register before `bind this_` below.
17 
18 struct v {
19     std::vector<int> nums;
20 
vv21     v(const std::vector<int>& numbers): nums(numbers) {}
22 
change_sign_allv23     void change_sign_all(const std::vector<int>& indices) {
24         void BOOST_LOCAL_FUNCTION(bind this_, int i) { // Bind object `this`.
25             this_->nums.at(i) = -this_->nums.at(i);
26         } BOOST_LOCAL_FUNCTION_NAME(complement)
27 
28         std::for_each(indices.begin(), indices.end(), complement);
29     }
30 };
31 
main(void)32 int main(void) {
33     std::vector<int> n(3);
34     n[0] = 1; n[1] = 2; n[2] = 3;
35 
36     std::vector<int> i(2);
37     i[0] = 0; i[1] = 2; // Will change n[0] and n[2] but not n[1].
38 
39     v vn(n);
40     vn.change_sign_all(i);
41 
42     BOOST_TEST(vn.nums.at(0) == -1);
43     BOOST_TEST(vn.nums.at(1) == 2);
44     BOOST_TEST(vn.nums.at(2) == -3);
45     return boost::report_errors();
46 }
47 
48