if_tests.cpp 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /*=============================================================================
  2. Copyright (c) 2001-2007 Joel de Guzman
  3. Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. ==============================================================================*/
  6. #include <iostream>
  7. #include <vector>
  8. #include <algorithm>
  9. #include <boost/detail/lightweight_test.hpp>
  10. #include <boost/phoenix/core.hpp>
  11. #include <boost/phoenix/statement.hpp>
  12. #include <boost/phoenix/operator.hpp>
  13. int
  14. main()
  15. {
  16. using boost::phoenix::arg_names::arg1;
  17. using boost::phoenix::if_;
  18. using boost::phoenix::ref;
  19. int init[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
  20. std::vector<int> v(init, init+10);
  21. std::cout << std::dec;
  22. int x = 0;
  23. for_each(v.begin(), v.end(),
  24. if_(arg1 > 3 && arg1 <= 8)
  25. [
  26. std::cout << arg1 << ", ",
  27. ref(x) += arg1
  28. ]
  29. );
  30. std::cout << std::endl;
  31. BOOST_TEST(x == 4+5+6+7+8);
  32. x = 0;
  33. int y = 0;
  34. int z = 0;
  35. for_each(v.begin(), v.end(),
  36. if_(arg1 > 5)
  37. [
  38. std::cout << arg1 << " > 5\n",
  39. ref(x) += arg1
  40. ]
  41. .else_
  42. [
  43. if_(arg1 == 5)
  44. [
  45. std::cout << arg1 << " == 5\n",
  46. ref(z) += arg1
  47. ]
  48. .else_
  49. [
  50. std::cout << arg1 << " < 5\n",
  51. ref(y) += arg1
  52. ]
  53. ]
  54. );
  55. std::cout << std::endl;
  56. BOOST_TEST(x == 6+7+8+9+10);
  57. BOOST_TEST(y == 1+2+3+4);
  58. BOOST_TEST(z == 5);
  59. return boost::report_errors();
  60. }