1<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 2<html> 3<head> 4<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 5<title>Rationale</title> 6<link rel="stylesheet" href="../../../doc/src/boostbook.css" type="text/css"> 7<meta name="generator" content="DocBook XSL Stylesheets V1.79.1"> 8<link rel="home" href="../index.html" title="The Boost C++ Libraries BoostBook Documentation Subset"> 9<link rel="up" href="../yap.html" title="Chapter 47. Boost.YAP"> 10<link rel="prev" href="../BOOST_YAP__1_3_48_8_2_7_12.html" title="Macro BOOST_YAP_USER_LITERAL_PLACEHOLDER_OPERATOR"> 11<link rel="next" href="../concept_check.html" title="Chapter 48. Boost.Concept_Check"> 12</head> 13<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> 14<table cellpadding="2" width="100%"><tr> 15<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../boost.png"></td> 16<td align="center"><a href="../../../index.html">Home</a></td> 17<td align="center"><a href="../../../libs/libraries.htm">Libraries</a></td> 18<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td> 19<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td> 20<td align="center"><a href="../../../more/index.htm">More</a></td> 21</tr></table> 22<hr> 23<div class="spirit-nav"> 24<a accesskey="p" href="../BOOST_YAP__1_3_48_8_2_7_12.html"><img src="../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../yap.html"><img src="../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="../concept_check.html"><img src="../../../doc/src/images/next.png" alt="Next"></a> 25</div> 26<div class="section"> 27<div class="titlepage"><div><div><h2 class="title" style="clear: both"> 28<a name="boost_yap.rationale"></a><a class="link" href="rationale.html" title="Rationale">Rationale</a> 29</h2></div></div></div> 30<h4> 31<a name="boost_yap.rationale.h0"></a> 32 <span class="phrase"><a name="boost_yap.rationale.decaying_values_captured_in_yap_expressions"></a></span><a class="link" href="rationale.html#boost_yap.rationale.decaying_values_captured_in_yap_expressions">Decaying 33 Values Captured in YAP Expressions</a> 34 </h4> 35<p> 36 The main objective of Boost.YAP is to be an easy-to-use and easy-to-understand 37 library for using the expression template programming technique. 38 </p> 39<p> 40 As such, it is very important that the way nodes in a Boost.YAP expression 41 tree are represented matches the way nodes in C++ builtin expression are represented. 42 This keeps the mental model for how to identify and manipulate parts of expression 43 trees consistent across C++ builtin and Boost.YAP trees. 44 </p> 45<p> 46 Though this creates minor difficulties (for instance, Boost.YAP terminals cannot 47 contain arrays), the benefit of a consistent programming model is more important. 48 </p> 49<h4> 50<a name="boost_yap.rationale.h1"></a> 51 <span class="phrase"><a name="boost_yap.rationale.reference_expressions"></a></span><a class="link" href="rationale.html#boost_yap.rationale.reference_expressions">Reference 52 Expressions</a> 53 </h4> 54<p> 55 Boost.YAP expressions can be used as subexpressions to build larger expressions. 56 <a class="link" href="../boost/yap/expr_kind.html#boost.yap.expr_kind.expr_ref"><code class="computeroutput"><span class="identifier">expr_kind</span><span class="special">::</span><span class="identifier">expr_ref</span></code></a> 57 exists because we want to be able to do this without incurring unnecessay copies 58 or moves. Consider <code class="computeroutput"><span class="identifier">v2</span></code> and 59 <code class="computeroutput"><span class="identifier">v3</span></code> in this snippet from the 60 Lazy Vector example. Each is a terminal that owns its value, rather than referring 61 to it. 62 </p> 63<pre class="programlisting"><span class="identifier">lazy_vector</span> <span class="identifier">v2</span><span class="special">{{</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special"><</span><span class="keyword">double</span><span class="special">>(</span><span class="number">4</span><span class="special">,</span> <span class="number">2.0</span><span class="special">)}};</span> 64<span class="identifier">lazy_vector</span> <span class="identifier">v3</span><span class="special">{{</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special"><</span><span class="keyword">double</span><span class="special">>(</span><span class="number">4</span><span class="special">,</span> <span class="number">3.0</span><span class="special">)}};</span> 65</pre> 66<p> 67 Now consider this expression: 68 </p> 69<pre class="programlisting"><span class="keyword">double</span> <span class="identifier">d1</span> <span class="special">=</span> <span class="special">(</span><span class="identifier">v2</span> <span class="special">+</span> <span class="identifier">v3</span><span class="special">)[</span><span class="number">2</span><span class="special">];</span> 70</pre> 71<p> 72 Without using reference semantics, how can we capture this expression, even 73 before evaluating it, without copying or moving the vectors? We cannot. We 74 must take references to the <code class="computeroutput"><span class="identifier">v2</span></code> 75 and <code class="computeroutput"><span class="identifier">v3</span></code> subexpressions to avoid 76 copying or moving. 77 </p> 78<p> 79 This comes at a cost. Dealing with <a class="link" href="../boost/yap/expr_kind.html#boost.yap.expr_kind.expr_ref"><code class="computeroutput"><span class="identifier">expr_kind</span><span class="special">::</span><span class="identifier">expr_ref</span></code></a> expressions complicates user 80 code. The alternatives, silently incurring copies/moves or disallowing the 81 use of subexpressions to build larger expressions, are worse. 82 </p> 83</div> 84<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> 85<td align="left"></td> 86<td align="right"><div class="copyright-footer">Copyright © 2018 T. Zachary Laine<p> 87 Distributed under the Boost Software License, Version 1.0. (See accompanying 88 file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>) 89 </p> 90</div></td> 91</tr></table> 92<hr> 93<div class="spirit-nav"> 94<a accesskey="p" href="../BOOST_YAP__1_3_48_8_2_7_12.html"><img src="../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../yap.html"><img src="../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="../concept_check.html"><img src="../../../doc/src/images/next.png" alt="Next"></a> 95</div> 96</body> 97</html> 98