transform.cpp 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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/detail/lightweight_test.hpp>
  12. #include <algorithm>
  13. #include <vector>
  14. int main(void) {
  15. //[transform
  16. int offset = 5;
  17. std::vector<int> v;
  18. std::vector<int> w;
  19. for(int i = 1; i <= 2; ++i) v.push_back(i * 10);
  20. BOOST_TEST(v[0] == 10); BOOST_TEST(v[1] == 20);
  21. w.resize(v.size());
  22. int BOOST_LOCAL_FUNCTION(const bind& offset, int i) {
  23. return ++i + offset;
  24. } BOOST_LOCAL_FUNCTION_NAME(inc)
  25. std::transform(v.begin(), v.end(), w.begin(), inc);
  26. BOOST_TEST(w[0] == 16); BOOST_TEST(w[1] == 26);
  27. int BOOST_LOCAL_FUNCTION(bind& inc, int i, int j) {
  28. return inc(i + j); // Call the other bound local function.
  29. } BOOST_LOCAL_FUNCTION_NAME(inc_sum)
  30. offset = 0;
  31. std::transform(v.begin(), v.end(), w.begin(), v.begin(), inc_sum);
  32. BOOST_TEST(v[0] == 27); BOOST_TEST(v[1] == 47);
  33. //]
  34. return boost::report_errors();
  35. }
  36. #endif // VARIADIC_MACROS