Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_33_1/doc/html/function/tutorial.html @ 12

Last change on this file since 12 was 12, checked in by landauf, 17 years ago

added boost

File size: 17.0 KB
Line 
1<html>
2<head>
3<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
4<title>Tutorial</title>
5<link rel="stylesheet" href="../boostbook.css" type="text/css">
6<meta name="generator" content="DocBook XSL Stylesheets V1.69.1">
7<link rel="start" href="../index.html" title="The Boost C++ Libraries">
8<link rel="up" href="../function.html" title="Chapter 4. Boost.Function">
9<link rel="prev" href="history.html" title="History &amp; Compatibility Notes">
10<link rel="next" href="reference.html" title="Reference">
11</head>
12<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
13<table cellpadding="2" width="100%">
14<td valign="top"><img alt="boost.png (6897 bytes)" width="277" height="86" src="../../../boost.png"></td>
15<td align="center"><a href="../../../index.htm">Home</a></td>
16<td align="center"><a href="../../../libs/libraries.htm">Libraries</a></td>
17<td align="center"><a href="../../../people/people.htm">People</a></td>
18<td align="center"><a href="../../../more/faq.htm">FAQ</a></td>
19<td align="center"><a href="../../../more/index.htm">More</a></td>
20</table>
21<hr>
22<div class="spirit-nav">
23<a accesskey="p" href="history.html"><img src="../images/prev.png" alt="Prev"></a><a accesskey="u" href="../function.html"><img src="../images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../images/home.png" alt="Home"></a><a accesskey="n" href="reference.html"><img src="../images/next.png" alt="Next"></a>
24</div>
25<div class="section" lang="en">
26<div class="titlepage"><div><div><h3 class="title">
27<a name="function.tutorial"></a>Tutorial</h3></div></div></div>
28<div class="toc"><dl>
29<dt><span class="section"><a href="tutorial.html#id2688219">Basic Usage</a></span></dt>
30<dt><span class="section"><a href="tutorial.html#id2688524">Free functions</a></span></dt>
31<dt><span class="section"><a href="tutorial.html#id2688553">Member functions</a></span></dt>
32<dt><span class="section"><a href="tutorial.html#id2688714">References to Function Objects</a></span></dt>
33<dt><span class="section"><a href="tutorial.html#id2688865">Comparing Boost.Function function objects</a></span></dt>
34</dl></div>
35<p> Boost.Function has two syntactical forms: the preferred form
36and the portable form. The preferred form fits more closely with the
37C++ language and reduces the number of separate template parameters
38that need to be considered, often improving readability; however, the
39preferred form is not supported on all platforms due to compiler
40bugs. The compatible form will work on all compilers supported by
41Boost.Function. Consult the table below to determine which syntactic
42form to use for your compiler.
43
44  </p>
45<div class="informaltable"><table class="table">
46<colgroup>
47<col>
48<col>
49</colgroup>
50<thead><tr>
51<th align="left">Preferred syntax</th>
52<th align="left">Portable syntax</th>
53</tr></thead>
54<tbody><tr>
55<td align="left"><div class="itemizedlist"><ul type="disc" compact>
56<li>GNU C++ 2.95.x, 3.0.x, 3.1.x</li>
57<li>Comeau C++ 4.2.45.2</li>
58<li>SGI MIPSpro 7.3.0</li>
59<li>Intel C++ 5.0, 6.0</li>
60<li>Compaq's cxx 6.2</li>
61<li>Microsoft Visual C++ 7.1</li>
62</ul></div></td>
63<td align="left"><div class="itemizedlist"><ul type="disc" compact>
64<li><span class="emphasis"><em>Any compiler supporting the preferred syntax</em></span></li>
65<li>Microsoft Visual C++ 6.0, 7.0</li>
66<li>Borland C++ 5.5.1</li>
67<li>Sun WorkShop 6 update 2 C++ 5.3</li>
68<li>Metrowerks CodeWarrior 8.1</li>
69</ul></div></td>
70</tr></tbody>
71</table></div>
72<p> If your compiler does not appear in this list, please try the preferred syntax and report your results to the Boost list so that we can keep this table up-to-date.</p>
73<div class="section" lang="en">
74<div class="titlepage"><div><div><h4 class="title">
75<a name="id2688219"></a>Basic Usage</h4></div></div></div>
76<p> A function wrapper is defined simply
77by instantiating the <code class="computeroutput">function</code> class
78template with the desired return type and argument types, formulated
79as a C++ function type. Any number of arguments may be supplied, up to
80some implementation-defined limit (10 is the default maximum). The
81following declares a function object wrapper
82<code class="computeroutput">f</code> that takes two
83<code class="computeroutput">int</code> parameters and returns a
84<code class="computeroutput">float</code>:
85
86  </p>
87<div class="informaltable"><table class="table">
88<colgroup>
89<col>
90<col>
91</colgroup>
92<thead><tr>
93<th align="left">Preferred syntax</th>
94<th align="left">Portable syntax</th>
95</tr></thead>
96<tbody><tr>
97<td align="left"><pre xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" class="table-programlisting"><code class="computeroutput"><a href="../boost/function.html" title="Class template function">boost::function</a></code>&lt;float (int x, int y)&gt; f;</pre></td>
98<td align="left"><pre xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" class="table-programlisting"><code class="computeroutput"><a href="../functionN.html" title="Class template functionN">boost::function2</a></code>&lt;float, int, int&gt; f;</pre></td>
99</tr></tbody>
100</table></div>
101<p> By default, function object wrappers are empty, so we can create a
102function object to assign to <code class="computeroutput">f</code>:
103
104</p>
105<pre class="programlisting">struct int_div {
106  float operator()(int x, int y) const { return ((float)x)/y; };
107};</pre>
108<pre class="programlisting">f = int_div();</pre>
109<p> Now we can use <code class="computeroutput">f</code> to execute
110the underlying function object
111<code class="computeroutput">int_div</code>:
112
113</p>
114<pre class="programlisting">std::cout &lt;&lt; f(5, 3) &lt;&lt; std::endl;</pre>
115<p> We are free to assign any compatible function object to
116<code class="computeroutput">f</code>. If
117<code class="computeroutput">int_div</code> had been declared to take two
118<code class="computeroutput">long</code> operands, the implicit
119conversions would have been applied to the arguments without any user
120interference. The only limit on the types of arguments is that they be
121CopyConstructible, so we can even use references and arrays:
122
123  </p>
124<div class="informaltable"><table class="table">
125<colgroup><col></colgroup>
126<thead><tr><th align="left">Preferred syntax</th></tr></thead>
127<tbody><tr><td align="left"><pre xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" class="table-programlisting"><code class="computeroutput"><a href="../boost/function.html" title="Class template function">boost::function</a></code>&lt;void (int values[], int n, int&amp; sum, float&amp; avg)&gt; sum_avg;</pre></td></tr></tbody>
128</table></div>
129<div class="informaltable"><table class="table">
130<colgroup><col></colgroup>
131<thead><tr><th align="left">Portable syntax</th></tr></thead>
132<tbody><tr><td align="left"><pre xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" class="table-programlisting"><code class="computeroutput"><a href="../functionN.html" title="Class template functionN">boost::function4</a></code>&lt;void, int*, int, int&amp;, float&amp;&gt; sum_avg;</pre></td></tr></tbody>
133</table></div>
134<pre class="programlisting">void do_sum_avg(int values[], int n, int&amp; sum, float&amp; avg)
135{
136  sum = 0;
137  for (int i = 0; i &lt; n; i++)
138    sum += values[i];
139  avg = (float)sum / n;
140}</pre>
141<pre class="programlisting">sum_avg = &amp;do_sum_avg;</pre>
142<p> Invoking a function object wrapper that does not actually
143contain a function object is a precondition violation, much like
144trying to call through a null function pointer, and will throw a <code class="computeroutput"><a href="../bad_function_call.html" title="Class bad_function_call">bad_function_call</a></code> exception). We can check for an
145empty function object wrapper by using it in a boolean context (it evaluates <code class="computeroutput">true</code> if the wrapper is not empty) or compare it against <code class="computeroutput">0</code>. For instance:
146</p>
147<pre class="programlisting">if (f)
148  std::cout &lt;&lt; f(5, 3) &lt;&lt; std::endl;
149else
150  std::cout &lt;&lt; "f has no target, so it is unsafe to call" &lt;&lt; std::endl;</pre>
151<p> Alternatively,
152<code class="computeroutput"><code class="computeroutput"><a href="../boost/function.html#id2396343-bb">empty</a></code>()</code>
153method will return whether or not the wrapper is empty.  </p>
154<p> Finally, we can clear out a function target by assigning it to <code class="computeroutput">0</code> or by calling the <code class="computeroutput"><code class="computeroutput"><a href="../boost/function.html#id2434373-bb">clear</a></code>()</code> member function, e.g.,
155</p>
156<pre class="programlisting">f = 0;</pre>
157</div>
158<div class="section" lang="en">
159<div class="titlepage"><div><div><h4 class="title">
160<a name="id2688524"></a>Free functions</h4></div></div></div>
161<p> Free function pointers can be considered singleton function objects with const function call operators, and can therefore be directly used with the function object wrappers:
162</p>
163<pre class="programlisting">float mul_ints(int x, int y) { return ((float)x) * y; }</pre>
164<pre class="programlisting">f = &amp;mul_ints;</pre>
165<p> Note that the <code class="computeroutput">&amp;</code> isn't really necessary unless you happen to be using Microsoft Visual C++ version 6. </p>
166</div>
167<div class="section" lang="en">
168<div class="titlepage"><div><div><h4 class="title">
169<a name="id2688553"></a>Member functions</h4></div></div></div>
170<p> In many systems, callbacks often call to member functions of a
171particular object. This is often referred to as "argument binding",
172and is beyond the scope of Boost.Function. The use of member functions
173directly, however, is supported, so the following code is valid:
174
175</p>
176<pre class="programlisting">struct X {
177  int foo(int);
178};</pre>
179<div class="informaltable"><table class="table">
180<colgroup>
181<col>
182<col>
183</colgroup>
184<thead><tr>
185<th align="left">Preferred syntax</th>
186<th align="left">Portable syntax</th>
187</tr></thead>
188<tbody><tr>
189<td align="left"><pre xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" class="table-programlisting"><code class="computeroutput"><a href="../boost/function.html" title="Class template function">boost::function</a></code>&lt;int (X*, int)&gt; f;
190
191f = &amp;X::foo;
192 
193X x;
194f(&amp;x, 5);</pre></td>
195<td align="left"><pre xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" class="table-programlisting"><code class="computeroutput"><a href="../functionN.html" title="Class template functionN">boost::function2</a></code>&lt;int, X*, int&gt; f;
196
197f = &amp;X::foo;
198 
199X x;
200f(&amp;x, 5);</pre></td>
201</tr></tbody>
202</table></div>
203<p> Several libraries exist that support argument binding. Three such libraries are summarized below:
204</p>
205<div class="itemizedlist"><ul type="disc">
206<li><p><a href="../../../libs/bind/index.html" target="_top">Bind</a>. This library allows binding of
207  arguments for any function object. It is lightweight and very
208  portable.</p></li>
209<li>
210<p>The C++ Standard library. Using
211  <code class="computeroutput">std::bind1st</code> and
212  <code class="computeroutput">std::mem_fun</code> together one can bind
213  the object of a pointer-to-member function for use with
214  Boost.Function:
215
216  </p>
217<div class="informaltable"><table class="table">
218<colgroup>
219<col>
220<col>
221</colgroup>
222<thead><tr>
223<th align="left">Preferred syntax</th>
224<th align="left">Portable syntax</th>
225</tr></thead>
226<tbody><tr>
227<td align="left"><pre xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" class="table-programlisting"><code class="computeroutput"><a href="../boost/function.html" title="Class template function">boost::function</a></code>&lt;int (int)&gt; f;
228X x;
229f = std::bind1st(
230      std::mem_fun(&amp;X::foo), &amp;x);
231f(5); // Call x.foo(5)</pre></td>
232<td align="left"><pre xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" class="table-programlisting"><code class="computeroutput"><a href="../functionN.html" title="Class template functionN">boost::function1</a></code>&lt;int, int&gt; f;
233X x;
234f = std::bind1st(
235      std::mem_fun(&amp;X::foo), &amp;x);
236f(5); // Call x.foo(5)</pre></td>
237</tr></tbody>
238</table></div>
239</li>
240<li><p>The <a href="../lambda.html" title="Chapter 6. Boost.Lambda">Lambda</a> library. This library provides a powerful composition mechanism to construct function objects that uses very natural C++ syntax. Lambda requires a compiler that is reasonably conformant to the C++ standard. </p></li>
241</ul></div>
242</div>
243<div class="section" lang="en">
244<div class="titlepage"><div><div><h4 class="title">
245<a name="id2688714"></a>References to Function Objects</h4></div></div></div>
246<p> In some cases it is
247  expensive (or semantically incorrect) to have Boost.Function clone a
248  function object. In such cases, it is possible to request that
249  Boost.Function keep only a reference to the actual function
250  object. This is done using the <code class="computeroutput">ref</code>
251  and <code class="computeroutput">cref</code> functions to wrap a
252  reference to a function object:
253
254  </p>
255<div class="informaltable"><table class="table">
256<colgroup>
257<col>
258<col>
259</colgroup>
260<thead><tr>
261<th align="left">Preferred syntax</th>
262<th align="left">Portable syntax</th>
263</tr></thead>
264<tbody><tr>
265<td align="left"><pre xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" class="table-programlisting">stateful_type a_function_object;
266<code class="computeroutput"><a href="../boost/function.html" title="Class template function">boost::function</a></code>&lt;int (int)&gt; f;
267f = <code class="computeroutput"><a href="../reference_wrapper.html#id2366489">boost::ref</a></code>(a_function_object);
268
269<code class="computeroutput"><a href="../boost/function.html" title="Class template function">boost::function</a></code>&lt;int (int)&gt; f2(f);</pre></td>
270<td align="left"><pre xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" class="table-programlisting">stateful_type a_function_object;
271<code class="computeroutput"><a href="../functionN.html" title="Class template functionN">boost::function1</a></code>&lt;int, int&gt; f;
272f = <code class="computeroutput"><a href="../reference_wrapper.html#id2366489">boost::ref</a></code>(a_function_object);
273
274<code class="computeroutput"><a href="../functionN.html" title="Class template functionN">boost::function1</a></code>&lt;int, int&gt; f2(f);</pre></td>
275</tr></tbody>
276</table></div>
277<p> Here, <code class="computeroutput">f</code> will not make a copy
278of <code class="computeroutput">a_function_object</code>, nor will
279<code class="computeroutput">f2</code> when it is targeted to
280<code class="computeroutput">f</code>'s reference to
281<code class="computeroutput">a_function_object</code>. Additionally, when
282using references to function objects, Boost.Function will not throw
283exceptions during assignment or construction.
284</p>
285</div>
286<div class="section" lang="en">
287<div class="titlepage"><div><div><h4 class="title">
288<a name="id2688865"></a>Comparing Boost.Function function objects</h4></div></div></div>
289<p>Function object wrappers can be compared via <code class="computeroutput">==</code>
290  or <code class="computeroutput">!=</code> against any function object that can be stored
291  within the wrapper. If the function object wrapper contains a
292  function object of that type, it will be compared against the given
293  function object (which must be either be
294  <a href="../EqualityComparable.html" title="Concept EqualityComparable">EqualityComparable</a> or have an overloaded <code class="computeroutput"><a href="../function_equal.html" title="Function template function_equal">boost::function_equal</a></code>). For instance:</p>
295<pre class="programlisting">int compute_with_X(X*, int);
296
297f = &amp;X::foo;
298assert(f == &amp;X::foo);
299assert(&amp;compute_with_X != f);</pre>
300<p>When comparing against an instance of
301   <code class="computeroutput"><a href="../reference_wrapper.html" title="Class template reference_wrapper">reference_wrapper</a></code>, the address
302   of the object in the
303   <code class="computeroutput"><a href="../reference_wrapper.html" title="Class template reference_wrapper">reference_wrapper</a></code> is compared
304   against the address of the object stored by the function object
305   wrapper:</p>
306<pre class="programlisting">a_stateful_object so1, so2;
307f = <code class="computeroutput"><a href="../reference_wrapper.html#id2366489">boost::ref</a></code>(so1);
308assert(f == <code class="computeroutput"><a href="../reference_wrapper.html#id2366489">boost::ref</a></code>(so1));
309assert(f == so1); <span class="emphasis"><em>// Only if a_stateful_object is <a href="../EqualityComparable.html" title="Concept EqualityComparable">EqualityComparable</a></em></span>
310assert(f != <code class="computeroutput"><a href="../reference_wrapper.html#id2366489">boost::ref</a></code>(so2));</pre>
311</div>
312</div>
313<table width="100%"><tr>
314<td align="left"><small><p>Last revised: March 18, 2005 at 04:54:32 GMT</p></small></td>
315<td align="right"><small>Copyright © 2001-2004 Douglas Gregor</small></td>
316</tr></table>
317<hr>
318<div class="spirit-nav">
319<a accesskey="p" href="history.html"><img src="../images/prev.png" alt="Prev"></a><a accesskey="u" href="../function.html"><img src="../images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../images/home.png" alt="Home"></a><a accesskey="n" href="reference.html"><img src="../images/next.png" alt="Next"></a>
320</div>
321</body>
322</html>
Note: See TracBrowser for help on using the repository browser.