rationale.qbk 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. [section Rationale]
  2. [heading Decaying Values Captured in YAP Expressions]
  3. The main objective of _yap_ is to be an easy-to-use and easy-to-understand
  4. library for using the _et_ programming technique.
  5. As such, it is very important that the way nodes in a _yap_ expression tree
  6. are represented matches the way nodes in C++ builtin expression are
  7. represented. This keeps the mental model for how to identify and manipulate
  8. parts of expression trees consistent across C++ builtin and _yap_ trees.
  9. Though this creates minor difficulties (for instance, _yap_ terminals cannot
  10. contain arrays), the benefit of a consistent programming model is more
  11. important.
  12. [heading Reference Expressions]
  13. _yap_ expressions can be used as subexpressions to build larger expressions.
  14. _expr_ref_ exists because we want to be able to do this without incurring
  15. unnecessay copies or moves. Consider `v2` and `v3` in this snippet from the
  16. Lazy Vector example. Each is a terminal that owns its value, rather than
  17. referring to it.
  18. lazy_vector v2{{std::vector<double>(4, 2.0)}};
  19. lazy_vector v3{{std::vector<double>(4, 3.0)}};
  20. Now consider this expression:
  21. double d1 = (v2 + v3)[2];
  22. Without using reference semantics, how can we capture this expression, even
  23. before evaluating it, without copying or moving the vectors? We cannot. We
  24. must take references to the `v2` and `v3` subexpressions to avoid copying or
  25. moving.
  26. This comes at a cost. Dealing with _expr_ref_ expressions complicates user
  27. code. The alternatives, silently incurring copies/moves or disallowing the
  28. use of subexpressions to build larger expressions, are worse.
  29. [endsect]