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/function.hpp>
15 #include <boost/typeof/typeof.hpp>
16 #include BOOST_TYPEOF_INCREMENT_REGISTRATION_GROUP()
17 #include <boost/detail/lightweight_test.hpp>
18 
19 BOOST_TYPEOF_REGISTER_TEMPLATE(boost::function, 1)
20 
derivative(boost::function<int (int)> & f,int dx)21 boost::function<int (int)> derivative(boost::function<int (int)>& f, int dx) {
22     int BOOST_LOCAL_FUNCTION(bind& f, const bind dx, int x) {
23         return (f(x + dx) - f(x)) / dx;
24     } BOOST_LOCAL_FUNCTION_NAME(deriv)
25 
26     return deriv;
27 }
28 
main(void)29 int main(void) {
30     int BOOST_LOCAL_FUNCTION(int x) {
31         return x + 4;
32     } BOOST_LOCAL_FUNCTION_NAME(add2)
33 
34     boost::function<int (int)> a2 = add2; // Reference valid where closure used.
35     boost::function<int (int)> d2 = derivative(a2, 2);
36     BOOST_TEST(d2(6) == 1);
37     return boost::report_errors();
38 }
39 
40 #endif // VARIADIC_MACROS
41 
42