add_typed_seq.cpp 977 B

12345678910111213141516171819202122232425262728293031323334353637
  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 <vector>
  9. #include <algorithm>
  10. struct adder {
  11. adder(void) : sum_(0) {}
  12. int sum(const std::vector<int>& nums, const int& factor = 10) {
  13. BOOST_LOCAL_FUNCTION( (const bind(const int&) factor)
  14. (bind(adder*) this_) (int num) (return int) ) {
  15. return this_->sum_ += factor * num;
  16. } BOOST_LOCAL_FUNCTION_NAME(add)
  17. std::for_each(nums.begin(), nums.end(), add);
  18. return sum_;
  19. }
  20. private:
  21. int sum_;
  22. };
  23. int main(void) {
  24. std::vector<int> v(3);
  25. v[0] = 1; v[1] = 2; v[2] = 3;
  26. BOOST_TEST(adder().sum(v) == 60);
  27. return boost::report_errors();
  28. }