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