1
0

function.tutorial.html 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>Tutorial</title><link rel="stylesheet" href="reference.css" type="text/css"><meta name="generator" content="DocBook XSL Stylesheets V1.61.3"><link rel="home" href="index.html" title="The Boost C++ Libraries"><link rel="up" href="function.html" title="Boost.Function"><link rel="previous" href="function.history.html" title="History &amp; Compatibility Notes"><link rel="next" href="function.reference.html" title="Reference"><link rel="chapter" href="libraries.html" title="The Boost C++ Libraries"><link rel="chapter" href="any.html" title="Boost.Any"><link rel="refentry" href="class.boost.bad_any_cast.html" title="Class bad_any_cast"><link rel="refentry" href="class.boost.any.html" title="Class any"><link rel="refentry" href="id2383300-bb.html" title="Function any_cast"><link rel="chapter" href="array.html" title="Boost.Array"><link rel="refentry" href="class.boost.array.html" title="Class template array"><link rel="chapter" href="function.html" title="Boost.Function"><link rel="refentry" href="class.boost.bad_function_call.html" title="Class bad_function_call"><link rel="refentry" href="class.boost.function_base.html" title="Class function_base"><link rel="refentry" href="class.boost.functionN.html" title="Class template functionN"><link rel="refentry" href="class.boost.function.html" title="Class template function"><link rel="chapter" href="ref.html" title="Boost.Ref"><link rel="refentry" href="class.boost.reference_wrapper.html" title="Class template reference_wrapper"><link rel="refentry" href="class.boost.is_reference_wrapper.html" title="Class template is_reference_wrapper"><link rel="refentry" href="class.boost.unwrap_reference.html" title="Class template unwrap_reference"><link rel="chapter" href="signals.html" title="Boost.Signals"><link rel="refentry" href="class.boost.signalN.html" title="Class template signalN"><link rel="refentry" href="class.boost.signal.html" title="Class template signal"><link rel="refentry" href="class.boost.slot.html" title="Class template slot"><link rel="refentry" href="class.boost.signals.trackable.html" title="Class trackable"><link rel="refentry" href="class.boost.signals.connection.html" title="Class connection"><link rel="refentry" href="class.boost.signals.scoped_connection.html" title="Class scoped_connection"><link rel="refentry" href="id2532119-bb.html" title="Function template visit_each"><link rel="refentry" href="class.boost.last_value.html" title="Class template last_value"><link rel="refentry" href="id2354683-bb.html" title="Class last_value&lt;void&gt;"><link rel="chapter" href="variant.html" title="Boost.Variant"><link rel="refentry" href="BOOST_VARIANT_LIMIT_TYPES.html" title="Macro BOOST_VARIANT_LIMIT_TYPES"><link rel="refentry" href="BOOST_VARIANT_ENUM_PARAMS.html" title="Macro BOOST_VARIANT_ENUM_PARAMS"><link rel="refentry" href="BOOST_VARIANT_ENUM_SHIFTED_PARAMS.html" title="Macro BOOST_VARIANT_ENUM_SHIFTED_PARAMS"><link rel="refentry" href="BOOST_VARIANT_NO_REFERENCE_SUPPORT.html" title="Macro BOOST_VARIANT_NO_REFERENCE_SUPPORT"><link rel="refentry" href="BOOST_VARIANT_NO_TYPE_SEQUENCE_SUPPORT.html" title="Macro BOOST_VARIANT_NO_TYPE_SEQUENCE_SUPPORT"><link rel="refentry" href="BOOST_VARIANT_NO_FULL_RECURSIVE_VARIANT_SUPPORT.html" title="Macro BOOST_VARIANT_NO_FULL_RECURSIVE_VARIANT_SUPPORT"><link rel="refentry" href="class.boost.variant.html" title="Class template variant"><link rel="refentry" href="id2518553-bb.html" title="Function template swap"><link rel="refentry" href="id2524211-bb.html" title="Function template operator&lt;&lt;"><link rel="refentry" href="class.boost.make_variant_over.html" title="Class template make_variant_over"><link rel="refentry" href="class.boost.make_recursive_variant.html" title="Class template make_recursive_variant"><link rel="refentry" href="class.boost.make_recursive_variant_over.html" title="Class template make_recursive_variant_over"><link rel="refentry" href="class.boost.recursive_wrapper.html" title="Class template recursive_wrapper"><link rel="refentry" href="class.boost.is_recursive_wrapper.html" title="Class template is_recursive_wrapper"><link rel="refentry" href="class.boost.unwrap_recursive_wrapper.html" title="Class template unwrap_recursive_wrapper"><link rel="refentry" href="class.boost.apply_visitor_delayed_t.html" title="Class template apply_visitor_delayed_t"><link rel="refentry" href="id2557051-bb.html" title="Function apply_visitor"><link rel="refentry" href="class.boost.bad_get.html" title="Class bad_get"><link rel="refentry" href="id2493238-bb.html" title="Function get"><link rel="refentry" href="class.boost.bad_visit.html" title="Class bad_visit"><link rel="refentry" href="class.boost.static_visitor.html" title="Class template static_visitor"><link rel="refentry" href="class.boost.visitor_ptr_t.html" title="Class template visitor_ptr_t"><link rel="refentry" href="id2544445-bb.html" title="Function template visitor_ptr"><link rel="chapter" href="boostbook.html" title="The BoostBook Documentation Format"><link rel="refentry" href="boostbook.dtd.class-specialization.html" title="
  2. BoostBook element class-specialization"><link rel="refentry" href="boostbook.dtd.link-test.html" title="
  3. BoostBook element link-test"><link rel="refentry" href="boostbook.dtd.link-fail-test.html" title="
  4. BoostBook element link-fail-test"><link rel="refentry" href="boostbook.dtd.typedef.html" title="
  5. BoostBook element typedef"><link rel="refentry" href="boostbook.dtd.static-constant.html" title="
  6. BoostBook element static-constant"><link rel="refentry" href="boostbook.dtd.code.html" title="
  7. BoostBook element code"><link rel="refentry" href="boostbook.dtd.destructor.html" title="
  8. BoostBook element destructor"><link rel="refentry" href="boostbook.dtd.template-type-parameter.html" title="
  9. BoostBook element template-type-parameter"><link rel="refentry" href="boostbook.dtd.description.html" title="
  10. BoostBook element description"><link rel="refentry" href="boostbook.dtd.librarylist.html" title="
  11. BoostBook element librarylist"><link rel="refentry" href="boostbook.dtd.library-reference.html" title="
  12. BoostBook element library-reference"><link rel="refentry" href="boostbook.dtd.boostbook.html" title="
  13. BoostBook element boostbook"><link rel="refentry" href="boostbook.dtd.union.html" title="
  14. BoostBook element union"><link rel="refentry" href="boostbook.dtd.inherit.html" title="
  15. BoostBook element inherit"><link rel="refentry" href="boostbook.dtd.template-varargs.html" title="
  16. BoostBook element template-varargs"><link rel="refentry" href="boostbook.dtd.source.html" title="
  17. BoostBook element source"><link rel="refentry" href="boostbook.dtd.function.html" title="
  18. BoostBook element function"><link rel="refentry" href="boostbook.dtd.postconditions.html" title="
  19. BoostBook element postconditions"><link rel="refentry" href="boostbook.dtd.compile-test.html" title="
  20. BoostBook element compile-test"><link rel="refentry" href="boostbook.dtd.method.html" title="
  21. BoostBook element method"><link rel="refentry" href="boostbook.dtd.snippet.html" title="
  22. BoostBook element snippet"><link rel="refentry" href="boostbook.dtd.constructor.html" title="
  23. BoostBook element constructor"><link rel="refentry" href="boostbook.dtd.namespace.html" title="
  24. BoostBook element namespace"><link rel="refentry" href="boostbook.dtd.if-fails.html" title="
  25. BoostBook element if-fails"><link rel="refentry" href="boostbook.dtd.free-function-group.html" title="
  26. BoostBook element free-function-group"><link rel="refentry" href="boostbook.dtd.functionname.html" title="
  27. BoostBook element functionname"><link rel="refentry" href="boostbook.dtd.librarycategory.html" title="
  28. BoostBook element librarycategory"><link rel="refentry" href="boostbook.dtd.notes.html" title="
  29. BoostBook element notes"><link rel="refentry" href="boostbook.dtd.data-member.html" title="
  30. BoostBook element data-member"><link rel="refentry" href="boostbook.dtd.specialization.html" title="
  31. BoostBook element specialization"><link rel="refentry" href="boostbook.dtd.union-specialization.html" title="
  32. BoostBook element union-specialization"><link rel="refentry" href="boostbook.dtd.throws.html" title="
  33. BoostBook element throws"><link rel="refentry" href="boostbook.dtd.template-arg.html" title="
  34. BoostBook element template-arg"><link rel="refentry" href="boostbook.dtd.method-group.html" title="
  35. BoostBook element method-group"><link rel="refentry" href="boostbook.dtd.requirement.html" title="
  36. BoostBook element requirement"><link rel="refentry" href="boostbook.dtd.precondition.html" title="
  37. BoostBook element precondition"><link rel="refentry" href="boostbook.dtd.paramtype.html" title="
  38. BoostBook element paramtype"><link rel="refentry" href="boostbook.dtd.using-class.html" title="
  39. BoostBook element using-class"><link rel="refentry" href="boostbook.dtd.run-test.html" title="
  40. BoostBook element run-test"><link rel="refentry" href="boostbook.dtd.librarypurpose.html" title="
  41. BoostBook element librarypurpose"><link rel="refentry" href="boostbook.dtd.copy-assignment.html" title="
  42. BoostBook element copy-assignment"><link rel="refentry" href="boostbook.dtd.run-fail-test.html" title="
  43. BoostBook element run-fail-test"><link rel="refentry" href="boostbook.dtd.template.html" title="
  44. BoostBook element template"><link rel="refentry" href="boostbook.dtd.compile-fail-test.html" title="
  45. BoostBook element compile-fail-test"><link rel="refentry" href="boostbook.dtd.returns.html" title="
  46. BoostBook element returns"><link rel="refentry" href="boostbook.dtd.default.html" title="
  47. BoostBook element default"><link rel="refentry" href="boostbook.dtd.parameter.html" title="
  48. BoostBook element parameter"><link rel="refentry" href="boostbook.dtd.signature.html" title="
  49. BoostBook element signature"><link rel="refentry" href="boostbook.dtd.overloaded-function.html" title="
  50. BoostBook element overloaded-function"><link rel="refentry" href="boostbook.dtd.class.html" title="
  51. BoostBook element class"><link rel="refentry" href="boostbook.dtd.librarycategorydef.html" title="
  52. BoostBook element librarycategorydef"><link rel="refentry" href="boostbook.dtd.type.html" title="
  53. BoostBook element type"><link rel="refentry" href="boostbook.dtd.enumvalue.html" title="
  54. BoostBook element enumvalue"><link rel="refentry" href="boostbook.dtd.overloaded-method.html" title="
  55. BoostBook element overloaded-method"><link rel="refentry" href="boostbook.dtd.programlisting.html" title="
  56. BoostBook element programlisting"><link rel="refentry" href="boostbook.dtd.complexity.html" title="
  57. BoostBook element complexity"><link rel="refentry" href="boostbook.dtd.purpose.html" title="
  58. BoostBook element purpose"><link rel="refentry" href="boostbook.dtd.template-nontype-parameter.html" title="
  59. BoostBook element template-nontype-parameter"><link rel="refentry" href="boostbook.dtd.library.html" title="
  60. BoostBook element library"><link rel="refentry" href="boostbook.dtd.librarycategorylist.html" title="
  61. BoostBook element librarycategorylist"><link rel="refentry" href="boostbook.dtd.using-namespace.html" title="
  62. BoostBook element using-namespace"><link rel="refentry" href="boostbook.dtd.struct-specialization.html" title="
  63. BoostBook element struct-specialization"><link rel="refentry" href="boostbook.dtd.struct.html" title="
  64. BoostBook element struct"><link rel="refentry" href="boostbook.dtd.lib.html" title="
  65. BoostBook element lib"><link rel="refentry" href="boostbook.dtd.enum.html" title="
  66. BoostBook element enum"><link rel="refentry" href="boostbook.dtd.requires.html" title="
  67. BoostBook element requires"><link rel="refentry" href="boostbook.dtd.effects.html" title="
  68. BoostBook element effects"><link rel="refentry" href="boostbook.dtd.libraryname.html" title="
  69. BoostBook element libraryname"><link rel="refentry" href="boostbook.dtd.libraryinfo.html" title="
  70. BoostBook element libraryinfo"><link rel="refentry" href="boostbook.dtd.testsuite.html" title="
  71. BoostBook element testsuite"><link rel="refentry" href="boostbook.dtd.header.html" title="
  72. BoostBook element header"><link rel="refentry" href="boostbook.dtd.rationale.html" title="
  73. BoostBook element rationale"><link rel="refentry" href="Assignable.html" title="Concept Assignable"><link rel="refentry" href="InputIterator.html" title="Concept InputIterator"><link rel="refentry" href="OutputIterator.html" title="Concept OutputIterator"><link rel="refentry" href="ForwardIterator.html" title="Concept ForwardIterator"><link rel="refentry" href="BidirectionalIterator.html" title="Concept BidirectionalIterator"><link rel="refentry" href="RandomAccessIterator.html" title="Concept RandomAccessIterator"><link rel="refentry" href="DefaultConstructible.html" title="Concept DefaultConstructible"><link rel="refentry" href="CopyConstructible.html" title="Concept CopyConstructible"><link rel="refentry" href="EqualityComparable.html" title="Concept EqualityComparable"><link rel="refentry" href="LessThanComparable.html" title="Concept LessThanComparable"><link rel="refentry" href="SignedInteger.html" title="Concept SignedInteger"><link rel="subsection" href="function.tutorial.html#id2511232" title="Basic Usage"><link rel="subsection" href="function.tutorial.html#id2511507" title="Free functions"><link rel="subsection" href="function.tutorial.html#id2511537" title="Member functions"><link rel="subsection" href="function.tutorial.html#id2511686" title="References to Functions"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" border="1" cellpadding="2" bgcolor="#007F7F"><tr><td bgcolor="#FFFFFF"><img src="../../c++boost.gif" alt="c++boost.gif (8819 bytes)" width="277" height="86"></td><td><a href="../../index.htm"><font color="#FFFFFF" size="4" face="Arial">Home</font></a></td><td><a href="libraries.html"><font color="#FFFFFF" size="4" face="Arial">Libraries</font></a></td><td><a href="../../people/people.htm"><font color="#FFFFFF" size="4" face="Arial">People</font></a></td><td><a href="../../more/faq.htm"><font color="#FFFFFF" size="4" face="Arial">FAQ</font></a></td><td><a href="../../more/index.htm"><font color="#FFFFFF" size="4" face="Arial">More</font></a></td></tr></table><div class="section" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="function.tutorial"></a>Tutorial</h3></div></div><div></div></div><div class="toc"><dl><dt><a href="function.tutorial.html#id2511232">Basic Usage</a></dt><dt><a href="function.tutorial.html#id2511507">Free functions</a></dt><dt><a href="function.tutorial.html#id2511537">Member functions</a></dt><dt><a href="function.tutorial.html#id2511686">References to Functions</a></dt></dl></div><p> Boost.Function has two syntactical forms: the preferred form
  74. and the portable form. The preferred form fits more closely with the
  75. C++ language and reduces the number of separate template parameters
  76. that need to be considered, often improving readability; however, the
  77. preferred form is not supported on all platforms due to compiler
  78. bugs. The compatible form will work on all compilers supported by
  79. Boost.Function. Consult the table below to determine which syntactic
  80. form to use for your compiler.
  81. </p><div class="informaltable"><table border="1"><colgroup><col><col></colgroup><thead><tr><th align="left">Preferred syntax</th><th align="left">Portable syntax</th></tr></thead><tbody><tr><td align="left"><div class="itemizedlist"><ul type="disc" compact><li>GNU C++ 2.95.x, 3.0.x, 3.1.x</li><li>Comeau C++ 4.2.45.2</li><li>SGI MIPSpro 7.3.0</li><li>Intel C++ 5.0, 6.0</li><li>Compaq's cxx 6.2</li><li>Microsoft Visual C++ 7.1</li></ul></div></td><td align="left"><div class="itemizedlist"><ul type="disc" compact><li><span class="emphasis"><em>Any compiler supporting the preferred syntax</em></span></li><li>Microsoft Visual C++ 6.0, 7.0</li><li>Borland C++ 5.5.1</li><li>Sun WorkShop 6 update 2 C++ 5.3</li><li>Metrowerks CodeWarrior 8.1</li></ul></div></td></tr></tbody></table></div><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><div class="section" lang="en"><div class="titlepage"><div><div><h4 class="title"><a name="id2511232"></a>Basic Usage</h4></div></div><div></div></div><p> A function wrapper is defined simply
  82. by instantiating the <tt class="computeroutput">function</tt> class
  83. template with the desired return type and argument types, formulated
  84. as a C++ function type. Any number of arguments may be supplied, up to
  85. some implementation-defined limit (10 is the default maximum). The
  86. following declares a function object wrapper
  87. <tt class="computeroutput">f</tt> that takes two
  88. <tt class="computeroutput">int</tt> parameters and returns a
  89. <tt class="computeroutput">float</tt>:
  90. </p><div class="informaltable"><table border="1"><colgroup><col><col></colgroup><thead><tr><th align="left">Preferred syntax</th><th align="left">Portable syntax</th></tr></thead><tbody><tr><td align="left"><pre class="programlisting"><a href="class.boost.function.html" title="Class template function">boost::function</a>&lt;float (int x, int y)&gt; f;</pre></td><td align="left"><pre class="programlisting"><a href="class.boost.functionN.html" title="Class template functionN">boost::function2</a>&lt;float, int, int&gt; f;</pre></td></tr></tbody></table></div><p> By default, function object wrappers are empty, so we can create a
  91. function object to assign to <tt class="computeroutput">f</tt>:
  92. </p><pre class="programlisting">struct int_div {
  93. float operator()(int x, int y) const { return ((float)x)/y; };
  94. };</pre><pre class="programlisting">f = int_div();</pre><p> Now we can use <tt class="computeroutput">f</tt> to execute
  95. the underlying function object
  96. <tt class="computeroutput">int_div</tt>:
  97. </p><pre class="programlisting">std::cout &lt;&lt; f(5, 3) &lt;&lt; std::endl;</pre><p> We are free to assign any compatible function object to
  98. <tt class="computeroutput">f</tt>. If
  99. <tt class="computeroutput">int_div</tt> had been declared to take two
  100. <tt class="computeroutput">long</tt> operands, the implicit
  101. conversions would have been applied to the arguments without any user
  102. interference. The only limit on the types of arguments is that they be
  103. CopyConstructible, so we can even use references and arrays:
  104. </p><div class="informaltable"><table border="1"><colgroup><col></colgroup><thead><tr><th align="left">Preferred syntax</th></tr></thead><tbody><tr><td align="left"><pre class="programlisting"><a href="class.boost.function.html" title="Class template function">boost::function</a>&lt;void (int values[], int n, int&amp; sum, float&amp; avg)&gt; sum_avg;</pre></td></tr></tbody></table></div><div class="informaltable"><table border="1"><colgroup><col></colgroup><thead><tr><th align="left">Portable syntax</th></tr></thead><tbody><tr><td align="left"><pre class="programlisting"><a href="class.boost.functionN.html" title="Class template functionN">boost::function4</a>&lt;void, int*, int, int&amp;, float&amp;&gt; sum_avg;</pre></td></tr></tbody></table></div><pre class="programlisting">void do_sum_avg(int values[], int n, int&amp; sum, float&amp; avg)
  105. {
  106. sum = 0;
  107. for (int i = 0; i &lt; n; i++)
  108. sum += values[i];
  109. avg = (float)sum / n;
  110. }</pre><pre class="programlisting">sum_avg = &amp;do_sum_avg;</pre><p> Invoking a function object wrapper that does not actually
  111. contain a function object is a precondition violation, much like
  112. trying to call through a null function pointer, and will throw a <a href="class.boost.bad_function_call.html" title="Class bad_function_call">bad_function_call</a> exception). We can check for an
  113. empty function object wrapper by using it in a boolean context (it evaluates <tt class="computeroutput">true</tt> if the wrapper is not empty) or compare it against <tt class="computeroutput">0</tt>. For instance:
  114. </p><pre class="programlisting">if (f)
  115. std::cout &lt;&lt; f(5, 3) &lt;&lt; std::endl;
  116. else
  117. std::cout &lt;&lt; "f has no target, so it is unsafe to call" &lt;&lt; std::endl;</pre><p> Alternatively,
  118. <tt class="computeroutput"><a href="class.boost.function.html#id2432398-bb">empty</a>()</tt>
  119. method will return whether or not the wrapper is empty. </p><p> Finally, we can clear out a function target by assigning it to <tt class="computeroutput">0</tt> or by calling the <tt class="computeroutput"><a href="class.boost.function.html#id2465894-bb">clear</a>()</tt> member function, e.g.,
  120. </p><pre class="programlisting">f = 0;</pre></div><div class="section" lang="en"><div class="titlepage"><div><div><h4 class="title"><a name="id2511507"></a>Free functions</h4></div></div><div></div></div><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:
  121. </p><pre class="programlisting">float mul_ints(int x, int y) { return ((float)x) * y; }</pre><pre class="programlisting">f = &amp;mul_ints;</pre><p> Note that the <tt class="computeroutput">&amp;</tt> isn't really necessary unless you happen to be using Microsoft Visual C++ version 6. </p></div><div class="section" lang="en"><div class="titlepage"><div><div><h4 class="title"><a name="id2511537"></a>Member functions</h4></div></div><div></div></div><p> In many systems, callbacks often call to member functions of a
  122. particular object. This is often referred to as "argument binding",
  123. and is beyond the scope of Boost.Function. The use of member functions
  124. directly, however, is supported, so the following code is valid:
  125. </p><pre class="programlisting">struct X {
  126. int foo(int);
  127. };</pre><div class="informaltable"><table border="1"><colgroup><col><col></colgroup><thead><tr><th align="left">Preferred syntax</th><th align="left">Portable syntax</th></tr></thead><tbody><tr><td align="left"><pre class="programlisting"><a href="class.boost.function.html" title="Class template function">boost::function</a>&lt;int (X*, int)&gt; f;
  128. f = &amp;X::foo;
  129. X x;
  130. f(&amp;x, 5);</pre></td><td align="left"><pre class="programlisting"><a href="class.boost.functionN.html" title="Class template functionN">boost::function2</a>&lt;int, X*, int&gt; f;
  131. f = &amp;X::foo;
  132. X x;
  133. f(&amp;x, 5);</pre></td></tr></tbody></table></div><p> Several libraries exist that support argument binding. Three such libraries are summarized below:
  134. </p><div class="itemizedlist"><ul type="disc"><li><p><a href="../../libs/bind/index.html" target="_top">Bind</a>. This library allows binding of
  135. arguments for any function object. It is lightweight and very
  136. portable.</p></li><li><p>The C++ Standard library. Using
  137. <tt class="computeroutput">std::bind1st</tt> and
  138. <tt class="computeroutput">std::mem_fun</tt> together one can bind
  139. the object of a pointer-to-member function for use with
  140. Boost.Function:
  141. </p><div class="informaltable"><table border="1"><colgroup><col><col></colgroup><thead><tr><th align="left">Preferred syntax</th><th align="left">Portable syntax</th></tr></thead><tbody><tr><td align="left"><pre class="programlisting"><a href="class.boost.function.html" title="Class template function">boost::function</a>&lt;int (int)&gt; f;
  142. X x;
  143. f = std::bind1st(
  144. std::mem_fun(&amp;X::foo), &amp;x);
  145. f(5); // Call x.foo(5)</pre></td><td align="left"><pre class="programlisting"><a href="class.boost.functionN.html" title="Class template functionN">boost::function1</a>&lt;int, int&gt; f;
  146. X x;
  147. f = std::bind1st(
  148. std::mem_fun(&amp;X::foo), &amp;x);
  149. f(5); // Call x.foo(5)</pre></td></tr></tbody></table></div></li><li><p>The <a href="../../libs/lambda/index.html" target="_top">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></ul></div></div><div class="section" lang="en"><div class="titlepage"><div><div><h4 class="title"><a name="id2511686"></a>References to Functions</h4></div></div><div></div></div><p> In some cases it is
  150. expensive (or semantically incorrect) to have Boost.Function clone a
  151. function object. In such cases, it is possible to request that
  152. Boost.Function keep only a reference to the actual function
  153. object. This is done using the <tt class="computeroutput">ref</tt>
  154. and <tt class="computeroutput">cref</tt> functions to wrap a
  155. reference to a function object:
  156. </p><div class="informaltable"><table border="1"><colgroup><col><col></colgroup><thead><tr><th align="left">Preferred syntax</th><th align="left">Portable syntax</th></tr></thead><tbody><tr><td align="left"><pre class="programlisting"> stateful_type a_function_object;
  157. <a href="class.boost.function.html" title="Class template function">boost::function</a>&lt;int (int)&gt; f;
  158. f = <a href="class.boost.reference_wrapper.html#id2471260-bb">boost::ref</a>(a_function_object);
  159. <a href="class.boost.function.html" title="Class template function">boost::function</a>&lt;int (int)&gt; f2(f);</pre></td><td align="left"><pre class="programlisting"> stateful_type a_function_object;
  160. <a href="class.boost.functionN.html" title="Class template functionN">boost::function1</a>&lt;int, int&gt; f;
  161. f = <a href="class.boost.reference_wrapper.html#id2471260-bb">boost::ref</a>(a_function_object);
  162. <a href="class.boost.functionN.html" title="Class template functionN">boost::function1</a>&lt;int, int&gt; f2(f);</pre></td></tr></tbody></table></div><p> Here, <tt class="computeroutput">f</tt> will not make a copy
  163. of <tt class="computeroutput">a_function_object</tt>, nor will
  164. <tt class="computeroutput">f2</tt> when it is targeted to
  165. <tt class="computeroutput">f</tt>'s reference to
  166. <tt class="computeroutput">a_function_object</tt>. Additionally, when
  167. using references to function objects, Boost.Function will not throw
  168. exceptions during assignment or construction.
  169. </p></div></div><table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr><td align="left"><small>Last revised: December 19, 2003 at 03:33:25 GMT</small></td><td align="right"><small>Copyright © 2001-2003 Douglas Gregor</small></td></tr></table><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="function.history.html"><img src="images/prev.png" alt="Prev"></a> </td><td width="20%" align="center"><a accesskey="u" href="function.html"><img src="images/up.png" alt="Up"></a></td><td width="40%" align="right"> <a accesskey="n" href="function.reference.html"><img src="images/next.png" alt="Next"></a></td></tr><tr><td width="40%" align="left" valign="top">History &amp; Compatibility Notes </td><td width="20%" align="center"><a accesskey="h" href="index.html"><img src="images/home.png" alt="Home"></a></td><td width="40%" align="right" valign="top"> Reference</td></tr></table></div></body></html>
粤ICP备19079148号