PageRenderTime 46ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/liflg/library-boost
HTML | 143 lines | 140 code | 3 blank | 0 comment | 0 complexity | db5672a2921139d7fb4f0a39ea5103df 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.79.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 class="toc">
  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. <dt><span class="section"><a href="boost_functional_overloadedfunction/acknowledgments.html">Acknowledgments</a></span></dt>
  55. </dl>
  56. </div>
  57. <p>
  58. This library allows to overload different functions into a single function object.
  59. </p>
  60. <div class="section">
  61. <div class="titlepage"><div><div><h2 class="title" style="clear: both">
  62. <a name="boost_functional_overloadedfunction.introduction"></a><a class="link" href="index.html#boost_functional_overloadedfunction.introduction" title="Introduction">Introduction</a>
  63. </h2></div></div></div>
  64. <p>
  65. Consider the following functions which have distinct signatures:
  66. </p>
  67. <p>
  68. </p>
  69. <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>
  70. <span class="special">{</span> <span class="keyword">return</span> <span class="identifier">x</span><span class="special">;</span> <span class="special">}</span>
  71. <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>
  72. <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>
  73. <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>
  74. <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>
  75. </pre>
  76. <p>
  77. </p>
  78. <p>
  79. Instead of calling them using their separate names (here <code class="computeroutput"><span class="identifier">BOOST_TEST</span></code>
  80. is equivalent to <code class="computeroutput"><span class="identifier">assert</span></code>):
  81. <a href="#ftn.boost_functional_overloadedfunction.introduction.f0" class="footnote" name="boost_functional_overloadedfunction.introduction.f0"><sup class="footnote">[1]</sup></a>
  82. </p>
  83. <p>
  84. </p>
  85. <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>
  86. <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>
  87. <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>
  88. </pre>
  89. <p>
  90. </p>
  91. <p>
  92. It is possible to use this library to create a single <a href="http://en.wikipedia.org/wiki/Function_overloading" target="_top">overloaded</a>
  93. function object (or <a href="http://en.wikipedia.org/wiki/Functor" target="_top">functor</a>)
  94. named <code class="computeroutput"><span class="identifier">identity</span></code> that aggregates
  95. together the calls to the specific functions (see also <a href="../../test/functor.cpp" target="_top"><code class="literal">functor.cpp</code></a>
  96. and <a href="../../test/identity.hpp" target="_top"><code class="literal">identity.hpp</code></a>):
  97. </p>
  98. <p>
  99. </p>
  100. <pre class="programlisting"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">overloaded_function</span><span class="special">&lt;</span>
  101. <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>
  102. <span class="special">,</span> <span class="keyword">int</span> <span class="special">(</span><span class="keyword">int</span><span class="special">)</span>
  103. <span class="special">,</span> <span class="keyword">double</span> <span class="special">(</span><span class="keyword">double</span><span class="special">)</span>
  104. <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>
  105. <span class="comment">// All calls via single `identity` function.</span>
  106. <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>
  107. <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>
  108. <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>
  109. </pre>
  110. <p>
  111. </p>
  112. <p>
  113. Note how the functions are called via a single overloaded function object
  114. <code class="computeroutput"><span class="identifier">identity</span></code> instead of using their
  115. different names <code class="computeroutput"><span class="identifier">identity_s</span></code>,
  116. <code class="computeroutput"><span class="identifier">identity_i</span></code>, and <code class="computeroutput"><span class="identifier">identity_d</span></code>.
  117. </p>
  118. </div>
  119. <div class="footnotes">
  120. <br><hr style="width:100; text-align:left;margin-left: 0">
  121. <div id="ftn.boost_functional_overloadedfunction.introduction.f0" class="footnote"><p><a href="#boost_functional_overloadedfunction.introduction.f0" class="para"><sup class="para">[1] </sup></a>
  122. In most of the examples presented in this documentation, the Boost.Core/LightweightTest
  123. (<code class="literal">boost/core/lightweight_test.hpp</code>) macro <code class="computeroutput"><span class="identifier">BOOST_TEST</span></code> is used to check correctness
  124. conditions (conceptually similar to <code class="computeroutput"><span class="identifier">assert</span></code>).
  125. A failure of the checked condition does not abort the execution of the program,
  126. it will instead make <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">report_errors</span></code>
  127. return a non-zero program exit code. Using Boost.Core/LightweightTest allows
  128. to add the examples to the library regression tests so to make sure that
  129. they always compile and run correctly.
  130. </p></div>
  131. </div>
  132. </div>
  133. <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
  134. <td align="left"><p><small>Last revised: December 05, 2018 at 20:06:59 GMT</small></p></td>
  135. <td align="right"><div class="copyright-footer"></div></td>
  136. </tr></table>
  137. <hr>
  138. <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>
  139. </body>
  140. </html>