PageRenderTime 6444ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/libs/functional/overloaded_function/doc/html/index.html

https://bitbucket.org/vaporoid/boost
HTML | 147 lines | 144 code | 3 blank | 0 comment | 0 complexity | 089fe44086e079e2f5c90d0367a50c1d MD5 | raw file
  1. <html>
  2. <head>
  3. <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
  4. <title>Chapter&#160;1.&#160;Boost.Functional/OverloadedFunction 1.0.0</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;Boost.Functional/OverloadedFunction 1.0.0">
  8. <link rel="next" href="boost_functional_overloadedfunction/getting_started.html" title="Getting Started">
  9. </head>
  10. <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
  11. <table cellpadding="2" width="100%"><tr>
  12. <td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../boost.png"></td>
  13. <td align="center"><a href="../../../../../index.html">Home</a></td>
  14. <td align="center"><a href="../../../../../libs/libraries.htm">Libraries</a></td>
  15. <td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
  16. <td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
  17. <td align="center"><a href="../../../../../more/index.htm">More</a></td>
  18. </tr></table>
  19. <hr>
  20. <div class="spirit-nav"><a accesskey="n" href="boost_functional_overloadedfunction/getting_started.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a></div>
  21. <div class="chapter">
  22. <div class="titlepage"><div>
  23. <div><h2 class="title">
  24. <a name="boost_functional_overloadedfunction"></a>Chapter&#160;1.&#160;Boost.Functional/OverloadedFunction 1.0.0</h2></div>
  25. <div><div class="author"><h3 class="author">
  26. <span class="firstname">Lorenzo</span> <span class="surname">Caminiti <code class="email">&lt;<a class="email" href="mailto:lorcaminiti@gmail.com">lorcaminiti@gmail.com</a>&gt;</code></span>
  27. </h3></div></div>
  28. <div><p class="copyright">Copyright &#169; 2011, 2012 Lorenzo Caminiti</p></div>
  29. <div><div class="legalnotice">
  30. <a name="boost_functional_overloadedfunction.legal"></a><p>
  31. Distributed under the Boost Software License, Version 1.0 (see accompanying
  32. 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>)
  33. </p>
  34. </div></div>
  35. </div></div>
  36. <div class="toc">
  37. <p><b>Table of Contents</b></p>
  38. <dl>
  39. <dt><span class="section"><a href="index.html#boost_functional_overloadedfunction.introduction">Introduction</a></span></dt>
  40. <dt><span class="section"><a href="boost_functional_overloadedfunction/getting_started.html">Getting
  41. Started</a></span></dt>
  42. <dd><dl>
  43. <dt><span class="section"><a href="boost_functional_overloadedfunction/getting_started.html#boost_functional_overloadedfunction.getting_started.compilers_and_platforms">Compilers
  44. and Platforms</a></span></dt>
  45. <dt><span class="section"><a href="boost_functional_overloadedfunction/getting_started.html#boost_functional_overloadedfunction.getting_started.installation">Installation</a></span></dt>
  46. </dl></dd>
  47. <dt><span class="section"><a href="boost_functional_overloadedfunction/tutorial.html">Tutorial</a></span></dt>
  48. <dd><dl>
  49. <dt><span class="section"><a href="boost_functional_overloadedfunction/tutorial.html#boost_functional_overloadedfunction.tutorial.overloading">Overloading</a></span></dt>
  50. <dt><span class="section"><a href="boost_functional_overloadedfunction/tutorial.html#boost_functional_overloadedfunction.tutorial.without_function_types">Without
  51. Function Types</a></span></dt>
  52. </dl></dd>
  53. <dt><span class="section"><a href="reference.html">Reference</a></span></dt>
  54. <dd><dl>
  55. <dt><span class="section"><a href="reference.html#header.boost.functional.overloaded_function_hpp">Header &lt;boost/functional/overloaded_function.hpp&gt;</a></span></dt>
  56. <dt><span class="section"><a href="reference.html#header.boost.functional.overloaded_function.config_hpp">Header &lt;boost/functional/overloaded_function/config.hpp&gt;</a></span></dt>
  57. </dl></dd>
  58. <dt><span class="section"><a href="boost_functional_overloadedfunction/acknowledgments.html">Acknowledgments</a></span></dt>
  59. </dl>
  60. </div>
  61. <p>
  62. This library allows to overload different functions into a single function object.
  63. </p>
  64. <div class="section boost_functional_overloadedfunction_introduction">
  65. <div class="titlepage"><div><div><h2 class="title" style="clear: both">
  66. <a name="boost_functional_overloadedfunction.introduction"></a><a class="link" href="index.html#boost_functional_overloadedfunction.introduction" title="Introduction">Introduction</a>
  67. </h2></div></div></div>
  68. <p>
  69. Consider the following functions which have distinct signatures:
  70. </p>
  71. <p>
  72. </p>
  73. <pre class="programlisting"><span class="keyword">const</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">&amp;</span> <span class="identifier">identity_s</span><span class="special">(</span><span class="keyword">const</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">&amp;</span> <span class="identifier">x</span><span class="special">)</span> <span class="comment">// Function (as pointer).</span>
  74. <span class="special">{</span> <span class="keyword">return</span> <span class="identifier">x</span><span class="special">;</span> <span class="special">}</span>
  75. <span class="keyword">int</span> <span class="identifier">identity_i_impl</span><span class="special">(</span><span class="keyword">int</span> <span class="identifier">x</span><span class="special">)</span> <span class="special">{</span> <span class="keyword">return</span> <span class="identifier">x</span><span class="special">;</span> <span class="special">}</span>
  76. <span class="keyword">int</span> <span class="special">(&amp;</span><span class="identifier">identity_i</span><span class="special">)(</span><span class="keyword">int</span><span class="special">)</span> <span class="special">=</span> <span class="identifier">identity_i_impl</span><span class="special">;</span> <span class="comment">// Function reference.</span>
  77. <span class="keyword">double</span> <span class="identifier">identity_d_impl</span><span class="special">(</span><span class="keyword">double</span> <span class="identifier">x</span><span class="special">)</span> <span class="special">{</span> <span class="keyword">return</span> <span class="identifier">x</span><span class="special">;</span> <span class="special">}</span>
  78. <span class="identifier">boost</span><span class="special">::</span><span class="identifier">function</span><span class="special">&lt;</span><span class="keyword">double</span> <span class="special">(</span><span class="keyword">double</span><span class="special">)&gt;</span> <span class="identifier">identity_d</span> <span class="special">=</span> <span class="identifier">identity_d_impl</span><span class="special">;</span> <span class="comment">// Functor.</span>
  79. </pre>
  80. <p>
  81. </p>
  82. <p>
  83. Instead of calling them using their separate names (here <code class="computeroutput"><span class="identifier">BOOST_TEST</span></code>
  84. is equivalent to <code class="computeroutput"><span class="identifier">assert</span></code>):
  85. <sup>[<a name="boost_functional_overloadedfunction.introduction.f0" href="#ftn.boost_functional_overloadedfunction.introduction.f0" class="footnote">1</a>]</sup>
  86. </p>
  87. <p>
  88. </p>
  89. <pre class="programlisting"><span class="identifier">BOOST_TEST</span><span class="special">(</span><span class="identifier">identity_s</span><span class="special">(</span><span class="string">"abc"</span><span class="special">)</span> <span class="special">==</span> <span class="string">"abc"</span><span class="special">);</span>
  90. <span class="identifier">BOOST_TEST</span><span class="special">(</span><span class="identifier">identity_i</span><span class="special">(</span><span class="number">123</span><span class="special">)</span> <span class="special">==</span> <span class="number">123</span><span class="special">);</span>
  91. <span class="identifier">BOOST_TEST</span><span class="special">(</span><span class="identifier">identity_d</span><span class="special">(</span><span class="number">1.23</span><span class="special">)</span> <span class="special">==</span> <span class="number">1.23</span><span class="special">);</span>
  92. </pre>
  93. <p>
  94. </p>
  95. <p>
  96. It is possible to use this library to create a single <a href="http://en.wikipedia.org/wiki/Function_overloading" target="_top">overloaded</a>
  97. function object (or <a href="http://en.wikipedia.org/wiki/Functor" target="_top">functor</a>)
  98. named <code class="computeroutput"><span class="identifier">identity</span></code> that aggregates
  99. together the calls to the specific functions (see also <a href="../../test/functor.cpp" target="_top"><code class="literal">functor.cpp</code></a>
  100. and <a href="../../test/identity.hpp" target="_top"><code class="literal">identity.hpp</code></a>):
  101. </p>
  102. <p>
  103. </p>
  104. <pre class="programlisting"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">overloaded_function</span><span class="special">&lt;</span>
  105. <span class="keyword">const</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">&amp;</span> <span class="special">(</span><span class="keyword">const</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">&amp;)</span>
  106. <span class="special">,</span> <span class="keyword">int</span> <span class="special">(</span><span class="keyword">int</span><span class="special">)</span>
  107. <span class="special">,</span> <span class="keyword">double</span> <span class="special">(</span><span class="keyword">double</span><span class="special">)</span>
  108. <span class="special">&gt;</span> <span class="identifier">identity</span><span class="special">(</span><span class="identifier">identity_s</span><span class="special">,</span> <span class="identifier">identity_i</span><span class="special">,</span> <span class="identifier">identity_d</span><span class="special">);</span>
  109. <span class="comment">// All calls via single `identity` function.</span>
  110. <span class="identifier">BOOST_TEST</span><span class="special">(</span><span class="identifier">identity</span><span class="special">(</span><span class="string">"abc"</span><span class="special">)</span> <span class="special">==</span> <span class="string">"abc"</span><span class="special">);</span>
  111. <span class="identifier">BOOST_TEST</span><span class="special">(</span><span class="identifier">identity</span><span class="special">(</span><span class="number">123</span><span class="special">)</span> <span class="special">==</span> <span class="number">123</span><span class="special">);</span>
  112. <span class="identifier">BOOST_TEST</span><span class="special">(</span><span class="identifier">identity</span><span class="special">(</span><span class="number">1.23</span><span class="special">)</span> <span class="special">==</span> <span class="number">1.23</span><span class="special">);</span>
  113. </pre>
  114. <p>
  115. </p>
  116. <p>
  117. Note how the functions are called via a single overloaded function object
  118. <code class="computeroutput"><span class="identifier">identity</span></code> instead of using their
  119. different names <code class="computeroutput"><span class="identifier">identity_s</span></code>,
  120. <code class="computeroutput"><span class="identifier">identity_i</span></code>, and <code class="computeroutput"><span class="identifier">identity_d</span></code>.
  121. </p>
  122. </div>
  123. <div class="footnotes">
  124. <br><hr width="100" align="left">
  125. <div class="footnote"><p><sup>[<a id="ftn.boost_functional_overloadedfunction.introduction.f0" href="#boost_functional_overloadedfunction.introduction.f0" class="para">1</a>] </sup>
  126. In most of the examples presented in this documentation, the Boost.Detail/LightweightTest
  127. (<code class="literal">boost/detail/lightweight_test.hpp</code>) macro <code class="computeroutput"><span class="identifier">BOOST_TEST</span></code> is used to check correctness
  128. conditions (conceptually similar to <code class="computeroutput"><span class="identifier">assert</span></code>).
  129. A failure of the checked condition does not abort the execution of the program,
  130. it will instead make <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">report_errors</span></code>
  131. return a non-zero program exit code. Using Boost.Detail/LightweightTest allows
  132. to add the examples to the library regression tests so to make sure that
  133. they always compile and run correctly.
  134. </p></div>
  135. </div>
  136. </div>
  137. <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
  138. <td align="left"><p><small>Last revised: April 28, 2012 at 02:19:13 GMT</small></p></td>
  139. <td align="right"><div class="copyright-footer"></div></td>
  140. </tr></table>
  141. <hr>
  142. <div class="spirit-nav"><a accesskey="n" href="boost_functional_overloadedfunction/getting_started.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a></div>
  143. </body>
  144. </html>