return_derivative_seq.cpp 1.0 KB

12345678910111213141516171819202122232425262728293031323334
  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/local_function.hpp>
  7. #include <boost/function.hpp>
  8. #include <boost/typeof/typeof.hpp>
  9. #include BOOST_TYPEOF_INCREMENT_REGISTRATION_GROUP()
  10. #include <boost/detail/lightweight_test.hpp>
  11. BOOST_TYPEOF_REGISTER_TEMPLATE(boost::function, 1)
  12. boost::function<int (int)> derivative(boost::function<int (int)>& f, int dx) {
  13. int BOOST_LOCAL_FUNCTION( (bind& f) (const bind dx) (int x) ) {
  14. return (f(x + dx) - f(x)) / dx;
  15. } BOOST_LOCAL_FUNCTION_NAME(deriv)
  16. return deriv;
  17. }
  18. int main(void) {
  19. int BOOST_LOCAL_FUNCTION( (int x) ) {
  20. return x + 4;
  21. } BOOST_LOCAL_FUNCTION_NAME(add2)
  22. boost::function<int (int)> a2 = add2;
  23. boost::function<int (int)> d2 = derivative(a2, 2);
  24. BOOST_TEST(d2(6) == 1);
  25. return boost::report_errors();
  26. }