add_template_seq.cpp 816 B

12345678910111213141516171819202122232425262728293031
  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/detail/lightweight_test.hpp>
  8. #include <algorithm>
  9. template<typename T>
  10. T total(const T& x, const T& y, const T& z) {
  11. T sum = T(), factor = 10;
  12. T BOOST_LOCAL_FUNCTION_TPL( (const bind factor) (bind& sum) (T num) ) {
  13. return sum += factor * num;
  14. } BOOST_LOCAL_FUNCTION_NAME_TPL(add)
  15. add(x);
  16. T nums[2]; nums[0] = y; nums[1] = z;
  17. std::for_each(nums, nums + 2, add);
  18. return sum;
  19. }
  20. int main(void) {
  21. BOOST_TEST(total(1, 2, 3) == 60);
  22. return boost::report_errors();
  23. }