PageRenderTime 28ms CodeModel.GetById 11ms app.highlight 13ms RepoModel.GetById 1ms app.codeStats 0ms

/Src/Dependencies/Boost/libs/phoenix/doc/html/phoenix/modules/core/arguments.html

http://hadesmem.googlecode.com/
HTML | 193 lines | 192 code | 1 blank | 0 comment | 0 complexity | 5d6ea6b9311851582813c52cd994f718 MD5 | raw file
  1<html>
  2<head>
  3<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
  4<title>Arguments</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="../core.html" title="Core">
  9<link rel="prev" href="references.html" title="References">
 10<link rel="next" href="nothing.html" title="Nothing">
 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="references.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="nothing.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.arguments"></a><a class="link" href="arguments.html" title="Arguments">Arguments</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">core</span><span class="special">/</span><span class="identifier">argument</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
 30</pre>
 31<p>
 32          We use an instance of:
 33        </p>
 34<pre class="programlisting"><span class="identifier">expression</span><span class="special">::</span><span class="identifier">argument</span><span class="special">&lt;</span><span class="identifier">N</span><span class="special">&gt;::</span><span class="identifier">type</span>
 35</pre>
 36<p>
 37          to represent the Nth function argument. The argument placeholder acts as
 38          an imaginary data-bin where a function argument will be placed.
 39        </p>
 40<a name="phoenix.modules.core.arguments.predefined_arguments"></a><h6>
 41<a name="id808492"></a>
 42          <a class="link" href="arguments.html#phoenix.modules.core.arguments.predefined_arguments">Predefined
 43          Arguments</a>
 44        </h6>
 45<p>
 46          There are a few predefined instances of <code class="computeroutput"><span class="identifier">expression</span><span class="special">::</span><span class="identifier">argument</span><span class="special">&lt;</span><span class="identifier">N</span><span class="special">&gt;::</span><span class="identifier">type</span></code>
 47          named <code class="computeroutput"><span class="identifier">arg1</span></code>..<code class="computeroutput"><span class="identifier">argN</span></code>, and its <a href="http://www.boost.org/libs/lambda/doc/index.html" target="_top">BLL</a>
 48          counterpart <code class="computeroutput"><span class="identifier">_1</span></code>..<code class="computeroutput"><span class="identifier">_N</span></code>. (where N is a predefined maximum).
 49        </p>
 50<p>
 51          Here are some sample preset definitions of <code class="computeroutput"><span class="identifier">arg1</span></code>..<code class="computeroutput"><span class="identifier">argN</span></code>
 52        </p>
 53<pre class="programlisting"><span class="keyword">namespace</span> <span class="identifier">placeholders</span>
 54<span class="special">{</span>
 55    <span class="identifier">expression</span><span class="special">::</span><span class="identifier">argument</span><span class="special">&lt;</span><span class="number">1</span><span class="special">&gt;::</span><span class="identifier">type</span> <span class="keyword">const</span> <span class="identifier">arg1</span> <span class="special">=</span> <span class="special">{};</span>
 56    <span class="identifier">expression</span><span class="special">::</span><span class="identifier">argument</span><span class="special">&lt;</span><span class="number">2</span><span class="special">&gt;::</span><span class="identifier">type</span> <span class="keyword">const</span> <span class="identifier">arg2</span> <span class="special">=</span> <span class="special">{};</span>
 57    <span class="identifier">expression</span><span class="special">::</span><span class="identifier">argument</span><span class="special">&lt;</span><span class="number">3</span><span class="special">&gt;::</span><span class="identifier">type</span> <span class="keyword">const</span> <span class="identifier">arg3</span> <span class="special">=</span> <span class="special">{};</span>
 58<span class="special">}</span>
 59</pre>
 60<p>
 61          and its <a href="http://www.boost.org/libs/lambda/doc/index.html" target="_top">BLL</a>
 62          <code class="computeroutput"><span class="identifier">_1</span></code>..<code class="computeroutput"><span class="identifier">_N</span></code>
 63          style counterparts:
 64        </p>
 65<pre class="programlisting"><span class="keyword">namespace</span> <span class="identifier">placeholders</span>
 66<span class="special">{</span>
 67    <span class="identifier">expression</span><span class="special">::</span><span class="identifier">argument</span><span class="special">&lt;</span><span class="number">1</span><span class="special">&gt;::</span><span class="identifier">type</span> <span class="keyword">const</span> <span class="identifier">_1</span> <span class="special">=</span> <span class="special">{};</span>
 68    <span class="identifier">expression</span><span class="special">::</span><span class="identifier">argument</span><span class="special">&lt;</span><span class="number">2</span><span class="special">&gt;::</span><span class="identifier">type</span> <span class="keyword">const</span> <span class="identifier">_2</span> <span class="special">=</span> <span class="special">{};</span>
 69    <span class="identifier">expression</span><span class="special">::</span><span class="identifier">argument</span><span class="special">&lt;</span><span class="number">3</span><span class="special">&gt;::</span><span class="identifier">type</span> <span class="keyword">const</span> <span class="identifier">_3</span> <span class="special">=</span> <span class="special">{};</span>
 70<span class="special">}</span>
 71</pre>
 72<div class="note"><table border="0" summary="Note">
 73<tr>
 74<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../../../../../doc/src/images/note.png"></td>
 75<th align="left">Note</th>
 76</tr>
 77<tr><td align="left" valign="top"><p>
 78            You can set <code class="computeroutput"><span class="identifier">BOOST_PHOENIX_ARG_LIMIT</span></code>,
 79            the predefined maximum placeholder index. By default, <code class="computeroutput"><span class="identifier">BOOST_PHOENIX_ARG_LIMIT</span></code>
 80            is set to <code class="computeroutput"><span class="identifier">BOOST_PHOENIX_LIMIT</span></code>
 81            (See <a class="link" href="../../actor.html" title="Actor">Actor</a>).
 82          </p></td></tr>
 83</table></div>
 84<a name="phoenix.modules.core.arguments.user_defined_arguments"></a><h6>
 85<a name="id809017"></a>
 86          <a class="link" href="arguments.html#phoenix.modules.core.arguments.user_defined_arguments">User
 87          Defined Arguments</a>
 88        </h6>
 89<p>
 90          When appropriate, you can define your own <code class="computeroutput"><span class="identifier">argument</span></code>
 91          names. For example:
 92        </p>
 93<pre class="programlisting"><span class="identifier">expression</span><span class="special">::</span><span class="identifier">argument</span><span class="special">&lt;</span><span class="number">1</span><span class="special">&gt;::</span><span class="identifier">type</span> <span class="identifier">x</span><span class="special">;</span> <span class="comment">// note one based index
 94</span></pre>
 95<p>
 96          <code class="computeroutput"><span class="identifier">x</span></code> may now be used as a
 97          parameter to a lazy function:
 98        </p>
 99<pre class="programlisting"><span class="identifier">add</span><span class="special">(</span><span class="identifier">x</span><span class="special">,</span> <span class="number">6</span><span class="special">)</span>
100</pre>
101<p>
102          which is equivalent to:
103        </p>
104<pre class="programlisting"><span class="identifier">add</span><span class="special">(</span><span class="identifier">arg1</span><span class="special">,</span> <span class="number">6</span><span class="special">)</span>
105</pre>
106<a name="phoenix.modules.core.arguments.evaluating_an_argument"></a><h6>
107<a name="id809168"></a>
108          <a class="link" href="arguments.html#phoenix.modules.core.arguments.evaluating_an_argument">Evaluating
109          an Argument</a>
110        </h6>
111<p>
112          An argument, when evaluated, selects the Nth argument from the those passed
113          in by the client.
114        </p>
115<p>
116          For example:
117        </p>
118<pre class="programlisting"><span class="keyword">char</span>        <span class="identifier">c</span> <span class="special">=</span> <span class="char">'A'</span><span class="special">;</span>
119<span class="keyword">int</span>         <span class="identifier">i</span> <span class="special">=</span> <span class="number">123</span><span class="special">;</span>
120<span class="keyword">const</span> <span class="keyword">char</span><span class="special">*</span> <span class="identifier">s</span> <span class="special">=</span> <span class="string">"Hello World"</span><span class="special">;</span>
121
122<span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="identifier">arg1</span><span class="special">(</span><span class="identifier">c</span><span class="special">)</span> <span class="special">&lt;&lt;</span> <span class="identifier">endl</span><span class="special">;</span>        <span class="comment">//  Get the 1st argument: c
123</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="identifier">arg1</span><span class="special">(</span><span class="identifier">i</span><span class="special">,</span> <span class="identifier">s</span><span class="special">)</span> <span class="special">&lt;&lt;</span> <span class="identifier">endl</span><span class="special">;</span>     <span class="comment">//  Get the 1st argument: i
124</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="identifier">arg2</span><span class="special">(</span><span class="identifier">i</span><span class="special">,</span> <span class="identifier">s</span><span class="special">)</span> <span class="special">&lt;&lt;</span> <span class="identifier">endl</span><span class="special">;</span>     <span class="comment">//  Get the 2nd argument: s
125</span></pre>
126<p>
127          will print out:
128        </p>
129<pre class="programlisting"><span class="identifier">A</span>
130<span class="number">123</span>
131<span class="identifier">Hello</span> <span class="identifier">World</span>
132</pre>
133<a name="phoenix.modules.core.arguments.extra_arguments"></a><h6>
134<a name="id809466"></a>
135          <a class="link" href="arguments.html#phoenix.modules.core.arguments.extra_arguments">Extra Arguments</a>
136        </h6>
137<p>
138          In C and C++, a function can have extra arguments that are not at all used
139          by the function body itself. These extra arguments are simply ignored.
140        </p>
141<p>
142          Phoenix also allows extra arguments to be passed. For example, recall our
143          original <code class="computeroutput"><span class="identifier">add</span></code> function:
144        </p>
145<pre class="programlisting"><span class="identifier">add</span><span class="special">(</span><span class="identifier">arg1</span><span class="special">,</span> <span class="identifier">arg2</span><span class="special">)</span>
146</pre>
147<p>
148          We know now that partially applying this function results to a function
149          that expects 2 arguments. However, the library is a bit more lenient and
150          allows the caller to supply more arguments than is actually required. Thus,
151          <code class="computeroutput"><span class="identifier">add</span></code> actually allows 2
152          <span class="emphasis"><em>or more</em></span> arguments. For instance, with:
153        </p>
154<pre class="programlisting"><span class="identifier">add</span><span class="special">(</span><span class="identifier">arg1</span><span class="special">,</span> <span class="identifier">arg2</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="identifier">z</span><span class="special">)</span>
155</pre>
156<p>
157          the third argument <code class="computeroutput"><span class="identifier">z</span></code> is
158          ignored. Taking this further, in-between arguments are also ignored. Example:
159        </p>
160<pre class="programlisting"><span class="identifier">add</span><span class="special">(</span><span class="identifier">arg1</span><span class="special">,</span> <span class="identifier">arg5</span><span class="special">)(</span><span class="identifier">a</span><span class="special">,</span> <span class="identifier">b</span><span class="special">,</span> <span class="identifier">c</span><span class="special">,</span> <span class="identifier">d</span><span class="special">,</span> <span class="identifier">e</span><span class="special">)</span>
161</pre>
162<p>
163          Here, arguments b, c, and d are ignored. The function <code class="computeroutput"><span class="identifier">add</span></code>
164          takes in the first argument (<code class="computeroutput"><span class="identifier">arg1</span></code>)
165          and the fifth argument (<code class="computeroutput"><span class="identifier">arg5</span></code>).
166        </p>
167<div class="note"><table border="0" summary="Note">
168<tr>
169<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../../../../../doc/src/images/note.png"></td>
170<th align="left">Note</th>
171</tr>
172<tr><td align="left" valign="top"><p>
173            There are a few reasons why enforcing strict arity is not desirable.
174            A case in point is the callback function. Typical callback functions
175            provide more information than is actually needed. Lambda functions are
176            often used as callbacks.
177          </p></td></tr>
178</table></div>
179</div>
180<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
181<td align="left"></td>
182<td align="right"><div class="copyright-footer">Copyright &#169; 2002-2005, 2010 Joel de Guzman, Dan Marsden, Thomas Heller<p>
183        Distributed under the Boost Software License, Version 1.0. (See accompanying
184        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>)
185      </p>
186</div></td>
187</tr></table>
188<hr>
189<div class="spirit-nav">
190<a accesskey="p" href="references.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="nothing.html"><img src="../../../../../../../doc/src/images/next.png" alt="Next"></a>
191</div>
192</body>
193</html>