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/function.hpp>
10 #include <boost/typeof/typeof.hpp>
11 #include BOOST_TYPEOF_INCREMENT_REGISTRATION_GROUP()
12 #include <boost/detail/lightweight_test.hpp>
13 
14 struct number;
15 BOOST_TYPEOF_REGISTER_TYPE(number) // Register before `bind this_` below.
16 
17 struct number {
numbernumber18     number(int value) : value_(value) {}
19 
incnumber20     boost::function<int (void)> inc(void) {
21         int BOOST_LOCAL_FUNCTION( (bind this_) ) {
22             return ++this_->value_;
23         } BOOST_LOCAL_FUNCTION_NAME(i)
24         return i;
25     }
26 
27 private:
28     int value_;
29 };
30 
main(void)31 int main(void) {
32     number n1 = 0; // Object valid in scope where closure is used.
33     boost::function<int (void)> inc1 = n1.inc();
34     number n2 = 0;
35     boost::function<int (void)> inc2 = n2.inc();
36 
37     BOOST_TEST(inc1() == 1);
38     BOOST_TEST(inc1() == 2);
39     BOOST_TEST(inc2() == 1);
40     BOOST_TEST(inc1() == 3);
41     return boost::report_errors();
42 }
43 
44