add_phoenix.cpp 998 B

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/spirit/include/phoenix.hpp>
  7. #include <boost/detail/lightweight_test.hpp>
  8. #include <algorithm>
  9. #include <iostream>
  10. //[add_phoenix
  11. int main(void) {
  12. using boost::phoenix::let;
  13. using boost::phoenix::local_names::_f;
  14. using boost::phoenix::cref;
  15. using boost::phoenix::ref;
  16. using boost::phoenix::arg_names::_1;
  17. int sum = 0, factor = 10;
  18. int nums[] = {1, 2, 3};
  19. // Passed to template, `factor` by constant, and defined in expression.
  20. std::for_each(nums, nums + 3, let(_f = cref(factor))[
  21. // Unfortunately, body cannot use C++ statement syntax.
  22. ref(sum) += _f * _1, _1 // Access `sum` by reference.
  23. ]);
  24. BOOST_TEST(sum == 60);
  25. return boost::report_errors();
  26. }
  27. //]