]>
Commit | Line | Data |
---|---|---|
1 | <html> | |
2 | <head> | |
3 | <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII"> | |
4 | <title>References</title> | |
5 | <link rel="stylesheet" href="../../../../../../../doc/src/boostbook.css" type="text/css"> | |
6 | <meta name="generator" content="DocBook XSL Stylesheets V1.75.2"> | |
7 | <link rel="home" href="../../../index.html" title="Chapter 1. Phoenix 3.2.0"> | |
8 | <link rel="up" href="../core.html" title="Core"> | |
9 | <link rel="prev" href="values.html" title="Values"> | |
10 | <link rel="next" href="arguments.html" title="Arguments"> | |
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="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> | |
24 | </div> | |
25 | <div class="section"> | |
26 | <div class="titlepage"><div><div><h4 class="title"> | |
27 | <a name="phoenix.modules.core.references"></a><a class="link" href="references.html" title="References">References</a> | |
28 | </h4></div></div></div> | |
29 | <pre class="programlisting"><span class="preprocessor">#include</span> <span class="special"><</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">></span> | |
30 | </pre> | |
31 | <p> | |
32 | Values are immutable constants. Attempting to modify a value will result | |
33 | in a compile time error. When we want the function to modify the parameter, | |
34 | we use a reference instead. For instance, imagine a lazy function <code class="computeroutput"><span class="identifier">add_assign</span></code>: | |
35 | </p> | |
36 | <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">&</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> | |
37 | </pre> | |
38 | <p> | |
39 | Here, we want the first function argument, x, to be mutable. Obviously, | |
40 | we cannot write: | |
41 | </p> | |
42 | <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> | |
43 | </pre> | |
44 | <p> | |
45 | In C++, we can pass in a reference to a variable as the first argument | |
46 | in our example above. Yet, by default, the library forces arguments passed | |
47 | to partially applied functions to be immutable values (see <a class="link" href="values.html" title="Values">Values</a>). | |
48 | To achieve our intent, we use: | |
49 | </p> | |
50 | <pre class="programlisting"><span class="identifier">expression</span><span class="special">::</span><span class="identifier">reference</span><span class="special"><</span><span class="identifier">T</span><span class="special">>::</span><span class="identifier">type</span> | |
51 | </pre> | |
52 | <p> | |
53 | This is similar to <code class="computeroutput"><span class="identifier">expression</span><span class="special">::</span><span class="identifier">value</span><span class="special"><</span><span class="identifier">T</span><span class="special">>::</span><span class="identifier">type</span></code> | |
54 | before but instead holds a reference to a variable. | |
55 | </p> | |
56 | <p> | |
57 | 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"><</span><span class="identifier">T</span><span class="special">>::</span><span class="identifier">type</span></code> | |
58 | objects directly. Instead we use: | |
59 | </p> | |
60 | <pre class="programlisting"><span class="identifier">ref</span><span class="special">(</span><span class="identifier">v</span><span class="special">)</span> | |
61 | </pre> | |
62 | <p> | |
63 | For example (where <code class="computeroutput"><span class="identifier">i</span></code> is | |
64 | an <code class="computeroutput"><span class="keyword">int</span></code> variable): | |
65 | </p> | |
66 | <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> | |
67 | </pre> | |
68 | <h6> | |
69 | <a name="phoenix.modules.core.references.h0"></a> | |
70 | <span><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 | |
71 | a Reference</a> | |
72 | </h6> | |
73 | <p> | |
74 | References are actors. Hence, references can be evaluated. Such invocation | |
75 | gives the reference's identity. Example: | |
76 | </p> | |
77 | <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> | |
78 | <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> | |
79 | <span class="identifier">cout</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="special"><<</span> <span class="identifier">ref</span><span class="special">(</span><span class="identifier">s</span><span class="special">)();</span> | |
80 | </pre> | |
81 | <p> | |
82 | prints out "3 Hello World" | |
83 | </p> | |
84 | <h6> | |
85 | <a name="phoenix.modules.core.references.h1"></a> | |
86 | <span><a name="phoenix.modules.core.references.constant_references"></a></span><a class="link" href="references.html#phoenix.modules.core.references.constant_references">Constant | |
87 | References</a> | |
88 | </h6> | |
89 | <p> | |
90 | Another free function | |
91 | </p> | |
92 | <pre class="programlisting"><span class="identifier">cref</span><span class="special">(</span><span class="identifier">cv</span><span class="special">)</span> | |
93 | </pre> | |
94 | <p> | |
95 | 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> | |
96 | creates an <code class="computeroutput"><span class="identifier">expression</span><span class="special">::</span><span class="identifier">reference</span><span class="special"><</span><span class="identifier">T</span> <span class="keyword">const</span><span class="special">>::</span><span class="identifier">type</span></code> | |
97 | 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"><</span><span class="identifier">T</span><span class="special">>::</span><span class="identifier">type</span></code> | |
98 | but when the data to be passed as argument to a function is heavy and expensive | |
99 | 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> | |
100 | offers a lighter alternative. | |
101 | </p> | |
102 | </div> | |
103 | <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> | |
104 | <td align="left"></td> | |
105 | <td align="right"><div class="copyright-footer">Copyright © 2002-2005, 2010, 2014, 2015 Joel de Guzman, Dan Marsden, Thomas | |
106 | Heller, John Fletcher<p> | |
107 | Distributed under the Boost Software License, Version 1.0. (See accompanying | |
108 | 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>) | |
109 | </p> | |
110 | </div></td> | |
111 | </tr></table> | |
112 | <hr> | |
113 | <div class="spirit-nav"> | |
114 | <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> | |
115 | </div> | |
116 | </body> | |
117 | </html> |