tutorial.xml 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <!--
  3. Copyright (c) 2002 Douglas Gregor <doug.gregor -at- gmail.com>
  4. Distributed under the Boost Software License, Version 1.0.
  5. (See accompanying file LICENSE_1_0.txt or copy at
  6. http://www.boost.org/LICENSE_1_0.txt)
  7. -->
  8. <!DOCTYPE library PUBLIC "-//Boost//DTD BoostBook XML V1.0//EN"
  9. "http://www.boost.org/tools/boostbook/dtd/boostbook.dtd">
  10. <section xmlns:xi="http://www.w3.org/2001/XInclude" id="function.tutorial"
  11. last-revision="$Date$">
  12. <title>Tutorial</title>
  13. <using-namespace name="boost"/>
  14. <para> Boost.Function has two syntactical forms: the preferred form
  15. and the portable form. The preferred form fits more closely with the
  16. C++ language and reduces the number of separate template parameters
  17. that need to be considered, often improving readability; however, the
  18. preferred form is not supported on all platforms due to compiler
  19. bugs. The compatible form will work on all compilers supported by
  20. Boost.Function. Consult the table below to determine which syntactic
  21. form to use for your compiler.
  22. <informaltable>
  23. <tgroup cols="2" align="left">
  24. <thead>
  25. <row>
  26. <entry>Preferred syntax</entry>
  27. <entry>Portable syntax</entry>
  28. </row>
  29. </thead>
  30. <tbody>
  31. <row>
  32. <entry>
  33. <itemizedlist spacing="compact">
  34. <listitem><simpara>GNU C++ 2.95.x, 3.0.x and later versions</simpara></listitem>
  35. <listitem><simpara>Comeau C++ 4.2.45.2</simpara></listitem>
  36. <listitem><simpara>SGI MIPSpro 7.3.0</simpara></listitem>
  37. <listitem><simpara>Intel C++ 5.0, 6.0</simpara></listitem>
  38. <listitem><simpara>Compaq's cxx 6.2</simpara></listitem>
  39. <listitem><simpara>Microsoft Visual C++ 7.1 and later versions</simpara></listitem>
  40. </itemizedlist>
  41. </entry>
  42. <entry>
  43. <itemizedlist spacing="compact">
  44. <listitem><simpara><emphasis>Any compiler supporting the preferred syntax</emphasis></simpara></listitem>
  45. <listitem><simpara>Microsoft Visual C++ 6.0, 7.0</simpara></listitem>
  46. <listitem><simpara>Borland C++ 5.5.1</simpara></listitem>
  47. <listitem><simpara>Sun WorkShop 6 update 2 C++ 5.3</simpara></listitem>
  48. <listitem><simpara>Metrowerks CodeWarrior 8.1</simpara></listitem>
  49. </itemizedlist>
  50. </entry>
  51. </row>
  52. </tbody>
  53. </tgroup>
  54. </informaltable>
  55. </para>
  56. <para> 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.</para>
  57. <using-class name="boost::function"/>
  58. <section>
  59. <title>Basic Usage</title> <para> A function wrapper is defined simply
  60. by instantiating the <computeroutput>function</computeroutput> class
  61. template with the desired return type and argument types, formulated
  62. as a C++ function type. Any number of arguments may be supplied, up to
  63. some implementation-defined limit (10 is the default maximum). The
  64. following declares a function object wrapper
  65. <computeroutput>f</computeroutput> that takes two
  66. <computeroutput>int</computeroutput> parameters and returns a
  67. <computeroutput>float</computeroutput>:
  68. <informaltable>
  69. <tgroup cols="2" align="left">
  70. <thead>
  71. <row>
  72. <entry>Preferred syntax</entry>
  73. <entry>Portable syntax</entry>
  74. </row>
  75. </thead>
  76. <tbody>
  77. <row>
  78. <entry>
  79. <programlisting name="function.tutorial.arith.cxx98"><classname>boost::function</classname>&lt;float (int x, int y)&gt; f;</programlisting>
  80. </entry>
  81. <entry>
  82. <programlisting name="function.tutorial.arith.portable"><classname alt="functionN">boost::function2</classname>&lt;float, int, int&gt; f;</programlisting>
  83. </entry>
  84. </row>
  85. </tbody>
  86. </tgroup>
  87. </informaltable>
  88. </para>
  89. <para> By default, function object wrappers are empty, so we can create a
  90. function object to assign to <computeroutput>f</computeroutput>:
  91. <programlisting name="function.tutorial.int_div">struct int_div {
  92. float operator()(int x, int y) const { return ((float)x)/y; };
  93. };</programlisting>
  94. <programlisting name="function.tutorial.use_int_div">f = int_div();</programlisting>
  95. </para>
  96. <para> Now we can use <computeroutput>f</computeroutput> to execute
  97. the underlying function object
  98. <computeroutput>int_div</computeroutput>:
  99. <programlisting name="function.tutorial.call_int_div">std::cout &lt;&lt; f(5, 3) &lt;&lt; std::endl;</programlisting>
  100. </para>
  101. <para> We are free to assign any compatible function object to
  102. <computeroutput>f</computeroutput>. If
  103. <computeroutput>int_div</computeroutput> had been declared to take two
  104. <computeroutput>long</computeroutput> operands, the implicit
  105. conversions would have been applied to the arguments without any user
  106. interference. The only limit on the types of arguments is that they be
  107. CopyConstructible, so we can even use references and arrays:
  108. <informaltable>
  109. <tgroup cols="1" align="left">
  110. <thead><row><entry>Preferred syntax</entry></row></thead>
  111. <tbody>
  112. <row>
  113. <entry>
  114. <programlisting name="function.tutorial.sum_avg_decl.cxx98"><classname>boost::function</classname>&lt;void (int values[], int n, int&amp; sum, float&amp; avg)&gt; sum_avg;</programlisting>
  115. </entry>
  116. </row>
  117. </tbody>
  118. </tgroup>
  119. </informaltable>
  120. <informaltable>
  121. <tgroup cols="1" align="left">
  122. <thead><row><entry>Portable syntax</entry></row></thead>
  123. <tbody>
  124. <row>
  125. <entry>
  126. <programlisting name="function.tutorial.sum_avg_decl.portable"><classname alt="functionN">boost::function4</classname>&lt;void, int*, int, int&amp;, float&amp;&gt; sum_avg;</programlisting>
  127. </entry>
  128. </row>
  129. </tbody>
  130. </tgroup>
  131. </informaltable>
  132. <programlisting name="function.tutorial.sum_avg">void do_sum_avg(int values[], int n, int&amp; sum, float&amp; avg)
  133. {
  134. sum = 0;
  135. for (int i = 0; i &lt; n; i++)
  136. sum += values[i];
  137. avg = (float)sum / n;
  138. }</programlisting>
  139. <programlisting name="function.tutorial.use_sum_avg">sum_avg = &amp;do_sum_avg;</programlisting>
  140. </para>
  141. <para> Invoking a function object wrapper that does not actually
  142. contain a function object is a precondition violation, much like
  143. trying to call through a null function pointer, and will throw a <classname>bad_function_call</classname> exception). We can check for an
  144. empty function object wrapper by using it in a boolean context (it evaluates <computeroutput>true</computeroutput> if the wrapper is not empty) or compare it against <computeroutput>0</computeroutput>. For instance:
  145. <programlisting name="function.tutorial.check_empty">if (f)
  146. std::cout &lt;&lt; f(5, 3) &lt;&lt; std::endl;
  147. else
  148. std::cout &lt;&lt; "f has no target, so it is unsafe to call" &lt;&lt; std::endl;</programlisting>
  149. </para>
  150. <para> Alternatively,
  151. <computeroutput><methodname>empty</methodname>()</computeroutput>
  152. method will return whether or not the wrapper is empty. </para>
  153. <para> Finally, we can clear out a function target by assigning it to <computeroutput>0</computeroutput> or by calling the <computeroutput><methodname>clear</methodname>()</computeroutput> member function, e.g.,
  154. <programlisting name="function.tutorial.clear">f = 0;</programlisting>
  155. </para>
  156. </section>
  157. <section>
  158. <title>Free functions</title>
  159. <para> 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:
  160. <programlisting name="function.tutorial.mul_ints">float mul_ints(int x, int y) { return ((float)x) * y; }</programlisting>
  161. <programlisting name="function.tutorial.use_mul_ints">f = &amp;mul_ints;</programlisting>
  162. </para>
  163. <para> Note that the <computeroutput>&amp;</computeroutput> isn't really necessary unless you happen to be using Microsoft Visual C++ version 6. </para>
  164. </section>
  165. <section>
  166. <title>Member functions</title>
  167. <para> In many systems, callbacks often call to member functions of a
  168. particular object. This is often referred to as "argument binding",
  169. and is beyond the scope of Boost.Function. The use of member functions
  170. directly, however, is supported, so the following code is valid:
  171. <programlisting name="function.tutorial.X">struct X {
  172. int foo(int);
  173. };</programlisting>
  174. <informaltable>
  175. <tgroup cols="2" align="left">
  176. <thead>
  177. <row>
  178. <entry>Preferred syntax</entry>
  179. <entry>Portable syntax</entry>
  180. </row>
  181. </thead>
  182. <tbody>
  183. <row>
  184. <entry>
  185. <programlisting name="function.tutorial.mem_fun.cxx98"><classname>boost::function</classname>&lt;int (X*, int)&gt; f;
  186. f = &amp;X::foo;
  187. X x;
  188. f(&amp;x, 5);</programlisting>
  189. </entry>
  190. <entry>
  191. <programlisting name="function.tutorial.mem_fun.portable"><classname alt="functionN">boost::function2</classname>&lt;int, X*, int&gt; f;
  192. f = &amp;X::foo;
  193. X x;
  194. f(&amp;x, 5);</programlisting>
  195. </entry>
  196. </row>
  197. </tbody>
  198. </tgroup>
  199. </informaltable>
  200. </para>
  201. <para> Several libraries exist that support argument binding. Three such libraries are summarized below:
  202. <itemizedlist>
  203. <listitem> <para><libraryname>Bind</libraryname>. This library allows binding of
  204. arguments for any function object. It is lightweight and very
  205. portable.</para></listitem>
  206. <listitem> <para>The C++ Standard library. Using
  207. <computeroutput>std::bind1st</computeroutput> and
  208. <computeroutput>std::mem_fun</computeroutput> together one can bind
  209. the object of a pointer-to-member function for use with
  210. Boost.Function:
  211. <informaltable>
  212. <tgroup cols="2" align="left">
  213. <thead>
  214. <row>
  215. <entry>Preferred syntax</entry>
  216. <entry>Portable syntax</entry>
  217. </row>
  218. </thead>
  219. <tbody>
  220. <row>
  221. <entry>
  222. <programlisting name="function.tutorial.std_bind.cxx98"> <classname>boost::function</classname>&lt;int (int)&gt; f;
  223. X x;
  224. f = std::bind1st(
  225. std::mem_fun(&amp;X::foo), &amp;x);
  226. f(5); // Call x.foo(5)</programlisting>
  227. </entry>
  228. <entry>
  229. <programlisting name="function.tutorial.std_bind.portable"> <classname alt="functionN">boost::function1</classname>&lt;int, int&gt; f;
  230. X x;
  231. f = std::bind1st(
  232. std::mem_fun(&amp;X::foo), &amp;x);
  233. f(5); // Call x.foo(5)</programlisting>
  234. </entry>
  235. </row>
  236. </tbody>
  237. </tgroup>
  238. </informaltable>
  239. </para>
  240. </listitem>
  241. <listitem><para>The <libraryname>Lambda</libraryname> 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. </para></listitem>
  242. </itemizedlist>
  243. </para>
  244. </section>
  245. <section>
  246. <title>References to Function Objects</title> <para> 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 <computeroutput>ref</computeroutput>
  251. and <computeroutput>cref</computeroutput> functions to wrap a
  252. reference to a function object:
  253. <informaltable>
  254. <tgroup cols="2" align="left">
  255. <thead>
  256. <row>
  257. <entry>Preferred syntax</entry>
  258. <entry>Portable syntax</entry>
  259. </row>
  260. </thead>
  261. <tbody>
  262. <row>
  263. <entry>
  264. <programlisting name="function.tutorial.ref.cxx98">stateful_type a_function_object;
  265. <classname>boost::function</classname>&lt;int (int)&gt; f;
  266. f = <functionname>boost::ref</functionname>(a_function_object);
  267. <classname>boost::function</classname>&lt;int (int)&gt; f2(f);</programlisting>
  268. </entry>
  269. <entry>
  270. <programlisting name="function.tutorial.ref.portable">stateful_type a_function_object;
  271. <classname alt="functionN">boost::function1</classname>&lt;int, int&gt; f;
  272. f = <functionname>boost::ref</functionname>(a_function_object);
  273. <classname alt="functionN">boost::function1</classname>&lt;int, int&gt; f2(f);</programlisting>
  274. </entry>
  275. </row>
  276. </tbody>
  277. </tgroup>
  278. </informaltable>
  279. </para>
  280. <para> Here, <computeroutput>f</computeroutput> will not make a copy
  281. of <computeroutput>a_function_object</computeroutput>, nor will
  282. <computeroutput>f2</computeroutput> when it is targeted to
  283. <computeroutput>f</computeroutput>'s reference to
  284. <computeroutput>a_function_object</computeroutput>. Additionally, when
  285. using references to function objects, Boost.Function will not throw
  286. exceptions during assignment or construction.
  287. </para>
  288. </section>
  289. <section>
  290. <title>Comparing Boost.Function function objects</title>
  291. <para>Function object wrappers can be compared via <code>==</code>
  292. or <code>!=</code> against any function object that can be stored
  293. within the wrapper. If the function object wrapper contains a
  294. function object of that type, it will be compared against the given
  295. function object (which must be either be
  296. <conceptname>EqualityComparable</conceptname> or have an overloaded <functionname>boost::function_equal</functionname>). For instance:</para>
  297. <programlisting name="function.tutorial.compare">int compute_with_X(X*, int);
  298. f = &amp;X::foo;
  299. assert(f == &amp;X::foo);
  300. assert(&amp;compute_with_X != f);</programlisting>
  301. <para>When comparing against an instance of
  302. <code><classname>reference_wrapper</classname></code>, the address
  303. of the object in the
  304. <code><classname>reference_wrapper</classname></code> is compared
  305. against the address of the object stored by the function object
  306. wrapper:</para>
  307. <programlisting name="function.tutorial.compare-ref">a_stateful_object so1, so2;
  308. f = <functionname>boost::ref</functionname>(so1);
  309. assert(f == <functionname>boost::ref</functionname>(so1));
  310. assert(f == so1); <emphasis>// Only if a_stateful_object is <conceptname>EqualityComparable</conceptname></emphasis>
  311. assert(f != <functionname>boost::ref</functionname>(so2));</programlisting>
  312. </section>
  313. </section>