return_derivative.cpp 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. // Copyright (C) 2009-2012 Lorenzo Caminiti
  2. // Distributed under the Boost Software License, Version 1.0
  3. // (see accompanying file LICENSE_1_0.txt or a copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. // Home at http://www.boost.org/libs/local_function
  6. #include <boost/config.hpp>
  7. #ifdef BOOST_NO_CXX11_VARIADIC_MACROS
  8. # error "variadic macros required"
  9. #else
  10. #include <boost/local_function.hpp>
  11. #include <boost/function.hpp>
  12. #include <boost/typeof/typeof.hpp>
  13. #include BOOST_TYPEOF_INCREMENT_REGISTRATION_GROUP()
  14. #include <boost/detail/lightweight_test.hpp>
  15. BOOST_TYPEOF_REGISTER_TEMPLATE(boost::function, 1)
  16. boost::function<int (int)> derivative(boost::function<int (int)>& f, int dx) {
  17. int BOOST_LOCAL_FUNCTION(bind& f, const bind dx, int x) {
  18. return (f(x + dx) - f(x)) / dx;
  19. } BOOST_LOCAL_FUNCTION_NAME(deriv)
  20. return deriv;
  21. }
  22. int main(void) {
  23. int BOOST_LOCAL_FUNCTION(int x) {
  24. return x + 4;
  25. } BOOST_LOCAL_FUNCTION_NAME(add2)
  26. boost::function<int (int)> a2 = add2; // Reference valid where closure used.
  27. boost::function<int (int)> d2 = derivative(a2, 2);
  28. BOOST_TEST(d2(6) == 1);
  29. return boost::report_errors();
  30. }
  31. #endif // VARIADIC_MACROS