• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<html>
2<head>
3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
4<title>Chapter 1. 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 1. 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 1. 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 © 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
72<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>
73<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>
74
75<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>
76<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>
77</pre>
78<p>
79    </p>
80<p>
81      Instead of calling them using their separate names (here <code class="computeroutput"><span class="identifier">BOOST_TEST</span></code>
82      is equivalent to <code class="computeroutput"><span class="identifier">assert</span></code>):
83      <a href="#ftn.boost_functional_overloadedfunction.introduction.f0" class="footnote" name="boost_functional_overloadedfunction.introduction.f0"><sup class="footnote">[1]</sup></a>
84    </p>
85<p>
86</p>
87<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>
88<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>
89<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>
90</pre>
91<p>
92    </p>
93<p>
94      It is possible to use this library to create a single <a href="http://en.wikipedia.org/wiki/Function_overloading" target="_top">overloaded</a>
95      function object (or <a href="http://en.wikipedia.org/wiki/Functor" target="_top">functor</a>)
96      named <code class="computeroutput"><span class="identifier">identity</span></code> that aggregates
97      together the calls to the specific functions (see also <a href="../../test/functor.cpp" target="_top"><code class="literal">functor.cpp</code></a>
98      and <a href="../../test/identity.hpp" target="_top"><code class="literal">identity.hpp</code></a>):
99    </p>
100<p>
101</p>
102<pre class="programlisting"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">overloaded_function</span><span class="special">&lt;</span>
103      <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>
104    <span class="special">,</span> <span class="keyword">int</span> <span class="special">(</span><span class="keyword">int</span><span class="special">)</span>
105    <span class="special">,</span> <span class="keyword">double</span> <span class="special">(</span><span class="keyword">double</span><span class="special">)</span>
106<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>
107
108<span class="comment">// All calls via single `identity` function.</span>
109<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>
110<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>
111<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>
112</pre>
113<p>
114    </p>
115<p>
116      Note how the functions are called via a single overloaded function object
117      <code class="computeroutput"><span class="identifier">identity</span></code> instead of using their
118      different names <code class="computeroutput"><span class="identifier">identity_s</span></code>,
119      <code class="computeroutput"><span class="identifier">identity_i</span></code>, and <code class="computeroutput"><span class="identifier">identity_d</span></code>.
120    </p>
121</div>
122<div class="footnotes">
123<br><hr style="width:100; text-align:left;margin-left: 0">
124<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>
125        In most of the examples presented in this documentation, the Boost.Core/LightweightTest
126        (<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
127        conditions (conceptually similar to <code class="computeroutput"><span class="identifier">assert</span></code>).
128        A failure of the checked condition does not abort the execution of the program,
129        it will instead make <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">report_errors</span></code>
130        return a non-zero program exit code. Using Boost.Core/LightweightTest allows
131        to add the examples to the library regression tests so to make sure that
132        they always compile and run correctly.
133      </p></div>
134</div>
135</div>
136<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
137<td align="left"><p><small>Last revised: August 11, 2020 at 15:01:10 GMT</small></p></td>
138<td align="right"><div class="copyright-footer"></div></td>
139</tr></table>
140<hr>
141<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>
142</body>
143</html>
144