]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/local_function/example/n2529_this.cpp
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
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>
16 BOOST_TYPEOF_REGISTER_TYPE(v
) // Register before `bind this_` below.
19 std::vector
<int> nums
;
21 v(const std::vector
<int>& numbers
): nums(numbers
) {}
23 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
)
28 std::for_each(indices
.begin(), indices
.end(), complement
);
33 std::vector
<int> n(3);
34 n
[0] = 1; n
[1] = 2; n
[2] = 3;
36 std::vector
<int> i(2);
37 i
[0] = 0; i
[1] = 2; // Will change n[0] and n[2] but not n[1].
40 vn
.change_sign_all(i
);
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();