lambda_tests_phx2.cpp 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 <cmath>
  8. #include <algorithm>
  9. #include <vector>
  10. #include <boost/detail/lightweight_test.hpp>
  11. #include <boost/phoenix/core.hpp>
  12. #include <boost/phoenix/operator.hpp>
  13. #include <boost/phoenix/scope.hpp>
  14. #include <boost/phoenix/function.hpp>
  15. namespace boost { namespace phoenix
  16. {
  17. struct for_each_impl
  18. {
  19. template <typename C, typename F>
  20. struct result
  21. {
  22. typedef void type;
  23. };
  24. template <typename C, typename F>
  25. void operator()(C& c, F f) const
  26. {
  27. std::for_each(c.begin(), c.end(), f);
  28. }
  29. };
  30. function<for_each_impl> const for_each = for_each_impl();
  31. struct push_back_impl
  32. {
  33. template <typename C, typename T>
  34. struct result
  35. {
  36. typedef void type;
  37. };
  38. template <typename C, typename T>
  39. void operator()(C& c, T& x) const
  40. {
  41. c.push_back(x);
  42. }
  43. };
  44. function<push_back_impl> const push_back = push_back_impl();
  45. }}
  46. using namespace boost::phoenix;
  47. using namespace boost::phoenix::arg_names;
  48. using namespace boost::phoenix::local_names;
  49. using namespace std;
  50. struct zzz {};
  51. int
  52. main()
  53. {
  54. {
  55. using boost::phoenix::for_each;
  56. int init[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
  57. std::vector<int> v(init, init+10);
  58. int x = 0;
  59. for_each(_1, lambda(_a = _2)[_a += _1])(v, x);
  60. BOOST_TEST(x == 55);
  61. }
  62. {
  63. using boost::phoenix::for_each;
  64. using boost::phoenix::push_back;
  65. int x = 10;
  66. std::vector<std::vector<int> > v(10);
  67. for_each(_1, lambda(_a = _2)[push_back(_1, _a)])(v, x);
  68. int y = 0;
  69. for_each(arg1, lambda[ref(y) += _1[0]])(v);
  70. BOOST_TEST(y == 100);
  71. }
  72. return boost::report_errors();
  73. }