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/preprocessor/cat.hpp> |
10 | #include <boost/detail/lightweight_test.hpp> |
11 | #include <iostream> |
12 | |
13 | #define LOCAL_INC_DEC(offset) \ |
14 | int BOOST_LOCAL_FUNCTION_ID(BOOST_PP_CAT(inc, __LINE__), /* unique ID */ \ |
15 | (const bind offset) (const int x) ) { \ |
16 | return x + offset; \ |
17 | } BOOST_LOCAL_FUNCTION_NAME(inc) \ |
18 | \ |
19 | int BOOST_LOCAL_FUNCTION_ID(BOOST_PP_CAT(dec, __LINE__), \ |
20 | (const bind offset) (const int x) ) { \ |
21 | return x - offset; \ |
22 | } BOOST_LOCAL_FUNCTION_NAME(dec) |
23 | |
24 | #define LOCAL_INC_DEC_TPL(offset) \ |
25 | T BOOST_LOCAL_FUNCTION_ID_TPL(BOOST_PP_CAT(inc, __LINE__), \ |
26 | (const bind offset) (const T x) ) { \ |
27 | return x + offset; \ |
28 | } BOOST_LOCAL_FUNCTION_NAME_TPL(inc) \ |
29 | \ |
30 | T BOOST_LOCAL_FUNCTION_ID_TPL(BOOST_PP_CAT(dec, __LINE__), \ |
31 | (const bind offset) (const T x) ) { \ |
32 | return x - offset; \ |
33 | } BOOST_LOCAL_FUNCTION_NAME_TPL(dec) |
34 | |
35 | template<typename T> |
36 | void f(T& delta) { |
37 | LOCAL_INC_DEC_TPL(delta) // Multiple local functions on same line. |
38 | BOOST_TEST(dec(inc(123)) == 123); |
39 | } |
40 | |
41 | int main(void) { |
42 | int delta = 10; |
43 | LOCAL_INC_DEC(delta) // Declare local functions on same line using `_ID`. |
44 | BOOST_TEST(dec(inc(123)) == 123); |
45 | f(delta); |
46 | return boost::report_errors(); |
47 | } |
48 | |
49 |