lambda.cpp 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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 <algorithm>
  8. #include <vector>
  9. #include <boost/phoenix/scope.hpp>
  10. #include <boost/phoenix/core.hpp>
  11. #include <boost/phoenix/operator.hpp>
  12. #include <boost/phoenix/function.hpp>
  13. namespace lazy_stuff
  14. {
  15. using boost::phoenix::function;
  16. struct for_each_impl
  17. {
  18. typedef void result_type;
  19. template <typename C, typename F>
  20. void operator()(C& c, F f) const
  21. {
  22. std::for_each(c.begin(), c.end(), f);
  23. }
  24. };
  25. function<for_each_impl> const for_each = for_each_impl();
  26. struct push_back_impl
  27. {
  28. typedef void result_type;
  29. template <typename C, typename T>
  30. void operator()(C& c, T& x) const
  31. {
  32. c.push_back(x);
  33. }
  34. };
  35. function<push_back_impl> const push_back = push_back_impl();
  36. }
  37. int
  38. main()
  39. {
  40. {
  41. using lazy_stuff::for_each;
  42. using lazy_stuff::push_back;
  43. using boost::phoenix::lambda;
  44. using boost::phoenix::arg_names::arg1;
  45. using boost::phoenix::arg_names::arg2;
  46. using boost::phoenix::local_names::_a;
  47. int x = 10;
  48. std::vector<std::vector<int> > v(10);
  49. for_each(arg1,
  50. lambda(_a = arg2)
  51. [
  52. push_back(arg1, _a)
  53. ]
  54. )
  55. (v, x);
  56. }
  57. return 0;
  58. }