PageRenderTime 44ms CodeModel.GetById 31ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

/Src/Dependencies/Boost/libs/phoenix/doc/html/phoenix/modules/scope/let.html

http://hadesmem.googlecode.com/
HTML | 182 lines | 178 code | 4 blank | 0 comment | 0 complexity | 22ef44d6511b804b3850e75a17d3671a MD5 | raw file
  1<html>
  2<head>
  3<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
  4<title>let</title>
  5<link rel="stylesheet" href="../../../../../../../doc/src/boostbook.css" type="text/css">
  6<meta name="generator" content="DocBook XSL Stylesheets V1.76.1">
  7<link rel="home" href="../../../index.html" title="Chapter&#160;1.&#160;Phoenix 3.0">
  8<link rel="up" href="../scope.html" title="Scope">
  9<link rel="prev" href="local_variables.html" title="Local Variables">
 10<link rel="next" href="lambda.html" title="lambda">
 11</head>
 12<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
 13<table cellpadding="2" width="100%"><tr>
 14<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../../boost.png"></td>
 15<td align="center"><a href="../../../../../../../index.html">Home</a></td>
 16<td align="center"><a href="../../../../../../../libs/libraries.htm">Libraries</a></td>
 17<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
 18<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
 19<td align="center"><a href="../../../../../../../more/index.htm">More</a></td>
 20</tr></table>
 21<hr>
 22<div class="spirit-nav">
 23<a accesskey="p" href="local_variables.html"><img src="../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../scope.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="lambda.html"><img src="../../../../../../../doc/src/images/next.png" alt="Next"></a>
 24</div>
 25<div class="section">
 26<div class="titlepage"><div><div><h4 class="title">
 27<a name="phoenix.modules.scope.let"></a><a class="link" href="let.html" title="let">let</a>
 28</h4></div></div></div>
 29<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">phoenix</span><span class="special">/</span><span class="identifier">scope</span><span class="special">/</span><span class="identifier">let</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
 30</pre>
 31<p>
 32          You declare local variables using the syntax:
 33        </p>
 34<pre class="programlisting"><span class="identifier">let</span><span class="special">(</span><span class="identifier">local</span><span class="special">-</span><span class="identifier">declarations</span><span class="special">)</span>
 35<span class="special">[</span>
 36    <span class="identifier">let</span><span class="special">-</span><span class="identifier">body</span>
 37<span class="special">]</span>
 38</pre>
 39<p>
 40          <code class="computeroutput"><span class="identifier">let</span></code> allows 1..N local variable
 41          declarations (where N == <code class="computeroutput"><span class="identifier">BOOST_PHOENIX_LOCAL_LIMIT</span></code>).
 42          Each declaration follows the form:
 43        </p>
 44<pre class="programlisting"><span class="identifier">local</span><span class="special">-</span><span class="identifier">id</span> <span class="special">=</span> <span class="identifier">lambda</span><span class="special">-</span><span class="identifier">expression</span>
 45</pre>
 46<div class="note"><table border="0" summary="Note">
 47<tr>
 48<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../../../../../doc/src/images/note.png"></td>
 49<th align="left">Note</th>
 50</tr>
 51<tr><td align="left" valign="top"><p>
 52            You can set <code class="computeroutput"><span class="identifier">BOOST_PHOENIX_LOCAL_LIMIT</span></code>,
 53            the predefined maximum local variable declarations in a let expression.
 54            By default, <code class="computeroutput"><span class="identifier">BOOST_PHOENIX_LOCAL_LIMIT</span></code>
 55            is set to <code class="computeroutput"><span class="identifier">BOOST_PHOENIX_LIMIT</span></code>.
 56          </p></td></tr>
 57</table></div>
 58<p>
 59          Example:
 60        </p>
 61<pre class="programlisting"><span class="identifier">let</span><span class="special">(</span><span class="identifier">_a</span> <span class="special">=</span> <span class="number">123</span><span class="special">,</span> <span class="identifier">_b</span> <span class="special">=</span> <span class="number">456</span><span class="special">)</span>
 62<span class="special">[</span>
 63    <span class="identifier">_a</span> <span class="special">+</span> <span class="identifier">_b</span>
 64<span class="special">]</span>
 65</pre>
 66<p>
 67          <span class="bold"><strong>Reference Preservation</strong></span>
 68        </p>
 69<p>
 70          The type of the local variable assumes the type of the lambda- expression.
 71          Type deduction is reference preserving. For example:
 72        </p>
 73<pre class="programlisting"><span class="identifier">let</span><span class="special">(</span><span class="identifier">_a</span> <span class="special">=</span> <span class="identifier">arg1</span><span class="special">,</span> <span class="identifier">_b</span> <span class="special">=</span> <span class="number">456</span><span class="special">)</span>
 74</pre>
 75<p>
 76          <code class="computeroutput"><span class="identifier">_a</span></code> assumes the type of
 77          <code class="computeroutput"><span class="identifier">arg1</span></code>: a reference to an
 78          argument, while <code class="computeroutput"><span class="identifier">_b</span></code> has
 79          type <code class="computeroutput"><span class="keyword">int</span></code>.
 80        </p>
 81<p>
 82          Consider this:
 83        </p>
 84<pre class="programlisting"><span class="keyword">int</span> <span class="identifier">i</span> <span class="special">=</span> <span class="number">1</span><span class="special">;</span>
 85
 86<span class="identifier">let</span><span class="special">(</span><span class="identifier">_a</span> <span class="special">=</span> <span class="identifier">arg1</span><span class="special">)</span>
 87<span class="special">[</span>
 88    <span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="special">--</span><span class="identifier">_a</span> <span class="special">&lt;&lt;</span> <span class="char">' '</span>
 89<span class="special">]</span>
 90<span class="special">(</span><span class="identifier">i</span><span class="special">);</span>
 91
 92<span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="identifier">i</span> <span class="special">&lt;&lt;</span> <span class="identifier">endl</span><span class="special">;</span>
 93</pre>
 94<p>
 95          the output of above is : 0 0
 96        </p>
 97<p>
 98          While with this:
 99        </p>
100<pre class="programlisting"><span class="keyword">int</span> <span class="identifier">i</span> <span class="special">=</span> <span class="number">1</span><span class="special">;</span>
101
102<span class="identifier">let</span><span class="special">(</span><span class="identifier">_a</span> <span class="special">=</span> <span class="identifier">val</span><span class="special">(</span><span class="identifier">arg1</span><span class="special">))</span>
103<span class="special">[</span>
104    <span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="special">--</span><span class="identifier">_a</span> <span class="special">&lt;&lt;</span> <span class="char">' '</span>
105<span class="special">]</span>
106<span class="special">(</span><span class="identifier">i</span><span class="special">);</span>
107
108<span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="identifier">i</span> <span class="special">&lt;&lt;</span> <span class="identifier">endl</span><span class="special">;</span>
109</pre>
110<p>
111          the output is : 0 1
112        </p>
113<p>
114          Reference preservation is necessary because we need to have L-value access
115          to outer lambda-scopes (especially the arguments). <code class="computeroutput"><span class="identifier">arg</span></code>s
116          and <code class="computeroutput"><span class="identifier">ref</span></code>s are L-values.
117          <code class="computeroutput"><span class="identifier">val</span></code>s are R-values.
118        </p>
119<p>
120          <span class="bold"><strong>Visibility</strong></span> <a name="phoenix.modules.scope.let.visibility"></a>
121        </p>
122<p>
123          The scope and lifetimes of the local variables is limited within the let-body.
124          <code class="computeroutput"><span class="identifier">let</span></code> blocks can be nested.
125          A local variable may hide an outer local variable. For example:
126        </p>
127<pre class="programlisting"><span class="identifier">let</span><span class="special">(</span><span class="identifier">_x</span> <span class="special">=</span> <span class="number">1</span><span class="special">,</span> <span class="identifier">_y</span> <span class="special">=</span> <span class="string">", World"</span><span class="special">)</span>
128<span class="special">[</span>
129    <span class="comment">// _x here is an int: 1
130</span>
131    <span class="identifier">let</span><span class="special">(</span><span class="identifier">_x</span> <span class="special">=</span> <span class="string">"Hello"</span><span class="special">)</span> <span class="comment">// hides the outer _x
132</span>    <span class="special">[</span>
133        <span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="identifier">_x</span> <span class="special">&lt;&lt;</span> <span class="identifier">_y</span> <span class="comment">// prints "Hello, World"
134</span>    <span class="special">]</span>
135<span class="special">]</span>
136</pre>
137<p>
138          The RHS (right hand side lambda-expression) of each local-declaration cannot
139          refer to any LHS local-id. At this point, the local-ids are not in scope
140          yet; they will only be in scope in the let-body. The code below is in error:
141        </p>
142<pre class="programlisting"><span class="identifier">let</span><span class="special">(</span>
143    <span class="identifier">_a</span> <span class="special">=</span> <span class="number">1</span>
144  <span class="special">,</span> <span class="identifier">_b</span> <span class="special">=</span> <span class="identifier">_a</span> <span class="comment">// Error: _a is not in scope yet
145</span><span class="special">)</span>
146<span class="special">[</span>
147    <span class="comment">// _a and _b's scope starts here
148</span>    <span class="comment">/*. body .*/</span>
149<span class="special">]</span>
150</pre>
151<p>
152          However, if an outer let scope is available, this will be searched. Since
153          the scope of the RHS of a local-declaration is the outer scope enclosing
154          the let, the RHS of a local-declaration can refer to a local variable of
155          an outer scope:
156        </p>
157<pre class="programlisting"><span class="identifier">let</span><span class="special">(</span><span class="identifier">_a</span> <span class="special">=</span> <span class="number">1</span><span class="special">)</span>
158<span class="special">[</span>
159    <span class="identifier">let</span><span class="special">(</span>
160        <span class="identifier">_a</span> <span class="special">=</span> <span class="number">1</span>
161      <span class="special">,</span> <span class="identifier">_b</span> <span class="special">=</span> <span class="identifier">_a</span> <span class="comment">// Ok. _a refers to the outer _a
162</span>    <span class="special">)</span>
163    <span class="special">[</span>
164        <span class="comment">/*. body .*/</span>
165    <span class="special">]</span>
166<span class="special">]</span>
167</pre>
168</div>
169<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
170<td align="left"></td>
171<td align="right"><div class="copyright-footer">Copyright &#169; 2002-2005, 2010 Joel de Guzman, Dan Marsden, Thomas Heller<p>
172        Distributed under the Boost Software License, Version 1.0. (See accompanying
173        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>)
174      </p>
175</div></td>
176</tr></table>
177<hr>
178<div class="spirit-nav">
179<a accesskey="p" href="local_variables.html"><img src="../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../scope.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="lambda.html"><img src="../../../../../../../doc/src/images/next.png" alt="Next"></a>
180</div>
181</body>
182</html>