blob: a1653a86af0178ea68d86c96522cea3d0be68dc2 [file] [log] [blame]
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>References</title>
<link rel="stylesheet" href="../../../../../../../doc/src/boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.78.1">
<link rel="home" href="../../../index.html" title="Chapter&#160;1.&#160;Phoenix 3.2.0">
<link rel="up" href="../core.html" title="Core">
<link rel="prev" href="values.html" title="Values">
<link rel="next" href="arguments.html" title="Arguments">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../../boost.png"></td>
<td align="center"><a href="../../../../../../../index.html">Home</a></td>
<td align="center"><a href="../../../../../../../libs/libraries.htm">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../../../../../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="values.html"><img src="../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../core.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="arguments.html"><img src="../../../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="phoenix.modules.core.references"></a><a class="link" href="references.html" title="References">References</a>
</h4></div></div></div>
<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">core</span><span class="special">/</span><span class="identifier">reference</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
</pre>
<p>
Values are immutable constants. Attempting to modify a value will result
in a compile time error. When we want the function to modify the parameter,
we use a reference instead. For instance, imagine a lazy function <code class="computeroutput"><span class="identifier">add_assign</span></code>:
</p>
<pre class="programlisting"><span class="keyword">void</span> <span class="identifier">add_assign</span><span class="special">(</span><span class="identifier">T</span><span class="special">&amp;</span> <span class="identifier">x</span><span class="special">,</span> <span class="identifier">T</span> <span class="identifier">y</span><span class="special">)</span> <span class="special">{</span> <span class="identifier">x</span> <span class="special">+=</span> <span class="identifier">y</span><span class="special">;</span> <span class="special">}</span> <span class="comment">// pseudo code</span>
</pre>
<p>
Here, we want the first function argument, x, to be mutable. Obviously,
we cannot write:
</p>
<pre class="programlisting"><span class="identifier">add_assign</span><span class="special">(</span><span class="number">1</span><span class="special">,</span> <span class="number">2</span><span class="special">)</span> <span class="comment">// error first argument is immutable</span>
</pre>
<p>
In C++, we can pass in a reference to a variable as the first argument
in our example above. Yet, by default, the library forces arguments passed
to partially applied functions to be immutable values (see <a class="link" href="values.html" title="Values">Values</a>).
To achieve our intent, we use:
</p>
<pre class="programlisting"><span class="identifier">expression</span><span class="special">::</span><span class="identifier">reference</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;::</span><span class="identifier">type</span>
</pre>
<p>
This is similar to <code class="computeroutput"><span class="identifier">expression</span><span class="special">::</span><span class="identifier">value</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;::</span><span class="identifier">type</span></code>
before but instead holds a reference to a variable.
</p>
<p>
We normally don't instantiate <code class="computeroutput"><span class="identifier">expression</span><span class="special">::</span><span class="identifier">reference</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;::</span><span class="identifier">type</span></code>
objects directly. Instead we use:
</p>
<pre class="programlisting"><span class="identifier">ref</span><span class="special">(</span><span class="identifier">v</span><span class="special">)</span>
</pre>
<p>
For example (where <code class="computeroutput"><span class="identifier">i</span></code> is
an <code class="computeroutput"><span class="keyword">int</span></code> variable):
</p>
<pre class="programlisting"><span class="identifier">add_assign</span><span class="special">(</span><span class="identifier">ref</span><span class="special">(</span><span class="identifier">i</span><span class="special">),</span> <span class="number">2</span><span class="special">)</span>
</pre>
<h6>
<a name="phoenix.modules.core.references.h0"></a>
<span class="phrase"><a name="phoenix.modules.core.references.evaluating_a_reference"></a></span><a class="link" href="references.html#phoenix.modules.core.references.evaluating_a_reference">Evaluating
a Reference</a>
</h6>
<p>
References are actors. Hence, references can be evaluated. Such invocation
gives the reference's identity. Example:
</p>
<pre class="programlisting"><span class="keyword">int</span> <span class="identifier">i</span> <span class="special">=</span> <span class="number">3</span><span class="special">;</span>
<span class="keyword">char</span> <span class="keyword">const</span><span class="special">*</span> <span class="identifier">s</span> <span class="special">=</span> <span class="string">"Hello World"</span><span class="special">;</span>
<span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="identifier">ref</span><span class="special">(</span><span class="identifier">i</span><span class="special">)()</span> <span class="special">&lt;&lt;</span> <span class="identifier">ref</span><span class="special">(</span><span class="identifier">s</span><span class="special">)();</span>
</pre>
<p>
prints out "3 Hello World"
</p>
<h6>
<a name="phoenix.modules.core.references.h1"></a>
<span class="phrase"><a name="phoenix.modules.core.references.constant_references"></a></span><a class="link" href="references.html#phoenix.modules.core.references.constant_references">Constant
References</a>
</h6>
<p>
Another free function
</p>
<pre class="programlisting"><span class="identifier">cref</span><span class="special">(</span><span class="identifier">cv</span><span class="special">)</span>
</pre>
<p>
may also be used. <code class="computeroutput"><span class="identifier">cref</span><span class="special">(</span><span class="identifier">cv</span><span class="special">)</span></code>
creates an <code class="computeroutput"><span class="identifier">expression</span><span class="special">::</span><span class="identifier">reference</span><span class="special">&lt;</span><span class="identifier">T</span> <span class="keyword">const</span><span class="special">&gt;::</span><span class="identifier">type</span></code>
object. This is similar to <code class="computeroutput"><span class="identifier">expression</span><span class="special">::</span><span class="identifier">value</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;::</span><span class="identifier">type</span></code>
but when the data to be passed as argument to a function is heavy and expensive
to copy by value, the <code class="computeroutput"><span class="identifier">cref</span><span class="special">(</span><span class="identifier">cv</span><span class="special">)</span></code>
offers a lighter alternative.
</p>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><div class="copyright-footer">Copyright &#169; 2002-2005, 2010, 2014, 2015 Joel de Guzman, Dan Marsden, Thomas
Heller, John Fletcher<p>
Distributed under the Boost Software License, Version 1.0. (See accompanying
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>)
</p>
</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="values.html"><img src="../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../core.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="arguments.html"><img src="../../../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
</body>
</html>