other_snippets.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // Copyright (C) 2016-2018 T. Zachary Laine
  2. //
  3. // Distributed under the Boost Software License, Version 1.0. (See
  4. // accompanying file LICENSE_1_0.txt or copy at
  5. // http://www.boost.org/LICENSE_1_0.txt)
  6. #include <boost/yap/yap.hpp>
  7. #include <boost/yap/print.hpp>
  8. #include <iostream>
  9. #include <cmath>
  10. //[ plus_sqrt_term_alias
  11. template <typename T>
  12. using term = boost::yap::terminal<boost::yap::expression, T>;
  13. //]
  14. void primer()
  15. {
  16. //[ plus_sqrt_yap_value
  17. //[ plus_sqrt_yap_type
  18. //[ plus_sqrt_yap_top_level_1
  19. boost::yap::expression<
  20. boost::yap::expr_kind::plus,
  21. boost::hana::tuple<
  22. //]
  23. //[ plus_sqrt_yap_lhs
  24. boost::yap::expression<
  25. boost::yap::expr_kind::call,
  26. boost::hana::tuple<
  27. boost::yap::expression<
  28. boost::yap::expr_kind::terminal,
  29. boost::hana::tuple<double (*)(double)>
  30. >,
  31. boost::yap::expression<
  32. boost::yap::expr_kind::terminal,
  33. boost::hana::tuple<double>
  34. >
  35. >
  36. >,
  37. //]
  38. //[ plus_sqrt_yap_rhs
  39. boost::yap::expression<
  40. boost::yap::expr_kind::terminal,
  41. boost::hana::tuple<float>
  42. >
  43. //]
  44. //[ plus_sqrt_yap_top_level_2
  45. >
  46. >
  47. //]
  48. //]
  49. yap_expr = term<double (*)(double)>{{std::sqrt}}(3.0) + 8.0f;
  50. //]
  51. //[ print_plus_sqrt_yap_value
  52. print(std::cout, yap_expr);
  53. //]
  54. }
  55. void foo ()
  56. {
  57. //[ assign_through_terminal
  58. int i = 0;
  59. auto expr = boost::yap::make_terminal(i) = 42;
  60. evaluate(expr);
  61. std::cout << i << "\n"; // Prints 42.
  62. //]
  63. }
  64. //[ print_decl
  65. struct thing {};
  66. //]
  67. void print_expr ()
  68. {
  69. //[ print_expr
  70. using namespace boost::yap::literals;
  71. auto const const_lvalue_terminal_containing_rvalue = boost::yap::make_terminal("lvalue terminal");
  72. double const d = 1.0;
  73. auto rvalue_terminal_containing_lvalue = boost::yap::make_terminal(d);
  74. auto thing_terminal = boost::yap::make_terminal(thing{});
  75. auto expr =
  76. 4_p +
  77. std::move(rvalue_terminal_containing_lvalue) * thing_terminal -
  78. const_lvalue_terminal_containing_rvalue;
  79. //]
  80. boost::yap::print(std::cout, expr) << "\n";
  81. }
  82. int main ()
  83. {
  84. primer();
  85. foo();
  86. print_expr();
  87. return 0;
  88. }