About
Contact
QtCreator
KDevelop
Solarized
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/config.hpp>
9
#
ifdef
BOOST_NO_CXX11_VARIADIC_MACROS
10
# error "variadic macros required"
11
#
else
12
13
#include
<boost/local_function.hpp>
14
#include
<boost/typeof/typeof.hpp>
15
#include
BOOST_TYPEOF_INCREMENT_REGISTRATION_GROUP
()
16
#include
<boost/detail/lightweight_test.hpp>
17
#include
<vector>
18
#include
<algorithm>
19
20
struct
adder
;
21
BOOST_TYPEOF_REGISTER_TYPE
(adder)
// Register before `bind this_` below.
22
23
//[add_this
24
struct
adder
{
25
adder
() :
sum_
(
0
) {}
26
27
int
sum
(
const
std::
vector
<
int
>&
nums
,
const
int
factor
=
10
) {
28
29
void
BOOST_LOCAL_FUNCTION
(
const
bind
factor
, bind this_,
int
num
) {
30
this_
->
sum_
+=
factor
*
num
;
// Use `this_` instead of `this`.
31
}
BOOST_LOCAL_FUNCTION_NAME
(
add
)
32
33
std::
for_each
(
first:
nums
.
begin
(),
last:
nums
.
end
(),
f:
add
);
34
return
sum_
;
35
}
36
37
private
:
38
int
sum_
;
39
};
40
//]
41
42
int
main
(
void
) {
43
std::
vector
<
int
>
v
(
3
);
44
v
[
0
]
=
1
;
v
[
1
]
=
2
;
v
[
2
]
=
3
;
45
46
BOOST_TEST
(
adder
(
).
sum
(
v
) ==
60
);
47
return
boost::
report_errors
();
48
}
49
50
#
endif
// VARIADIC_MACROS
51
52