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