for_each.cpp 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. /*=============================================================================
  2. Copyright (c) 2005-2007 Dan Marsden
  3. Copyright (c) 2005-2007 Joel de Guzman
  4. Copyright (c) 2014 John Fletcher
  5. Distributed under the Boost Software License, Version 1.0. (See accompanying
  6. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. ==============================================================================*/
  8. #include <boost/phoenix.hpp>
  9. #include <boost/phoenix/core.hpp>
  10. #include <boost/phoenix/stl/algorithm/iteration.hpp>
  11. #include <boost/detail/lightweight_test.hpp>
  12. #include <functional>
  13. #include <vector>
  14. #include <string>
  15. #include <sstream>
  16. namespace
  17. {
  18. void for_each_test()
  19. {
  20. using boost::phoenix::for_each;
  21. using boost::phoenix::lambda;
  22. using boost::phoenix::val;
  23. using boost::phoenix::arg_names::arg1;
  24. std::vector<int> v;
  25. for (int i = 1; i < 10; i++)
  26. v.push_back(i);
  27. std::string test_str("(123456789)");
  28. std::ostringstream out;
  29. (
  30. out << val("("),
  31. for_each(arg1, lambda[out << arg1]),
  32. out << val(")")
  33. )(v);
  34. BOOST_TEST(out.str() == test_str);
  35. return;
  36. }
  37. }
  38. int main()
  39. {
  40. for_each_test();
  41. boost::report_errors();
  42. }