1<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 2<html> 3<head> 4<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 5<title>Functions with Multiple Arguments</title> 6<link rel="stylesheet" href="../../../doc/src/boostbook.css" type="text/css"> 7<meta name="generator" content="DocBook XSL Stylesheets V1.79.1"> 8<link rel="home" href="../index.html" title="The Boost C++ Libraries BoostBook Documentation Subset"> 9<link rel="up" href="../boost_typeerasure.html" title="Chapter 40. Boost.TypeErasure"> 10<link rel="prev" href="composing_concepts.html" title="Composing Concepts"> 11<link rel="next" href="concept.html" title="Concepts in Depth"> 12</head> 13<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> 14<table cellpadding="2" width="100%"><tr> 15<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../boost.png"></td> 16<td align="center"><a href="../../../index.html">Home</a></td> 17<td align="center"><a href="../../../libs/libraries.htm">Libraries</a></td> 18<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td> 19<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td> 20<td align="center"><a href="../../../more/index.htm">More</a></td> 21</tr></table> 22<hr> 23<div class="spirit-nav"> 24<a accesskey="p" href="composing_concepts.html"><img src="../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../boost_typeerasure.html"><img src="../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="concept.html"><img src="../../../doc/src/images/next.png" alt="Next"></a> 25</div> 26<div class="section"> 27<div class="titlepage"><div><div><h2 class="title" style="clear: both"> 28<a name="boost_typeerasure.multi"></a><a class="link" href="multi.html" title="Functions with Multiple Arguments">Functions with Multiple Arguments</a> 29</h2></div></div></div> 30<p> 31 (For the source of the examples in this section see <a href="../../../libs/type_erasure/example/multi.cpp" target="_top">multi.cpp</a>) 32 </p> 33<p> 34 Operations can have more than one <code class="computeroutput"><a class="link" href="../boost/type_erasure/any.html" title="Class template any">any</a></code> 35 argument. Let's use binary addition as an example. 36 </p> 37<p> 38</p> 39<pre class="programlisting"><span class="keyword">typedef</span> <span class="identifier">any</span><span class="special"><</span> 40 <span class="identifier">mpl</span><span class="special">::</span><span class="identifier">vector</span><span class="special"><</span> 41 <span class="identifier">copy_constructible</span><span class="special"><>,</span> 42 <span class="identifier">typeid_</span><span class="special"><>,</span> 43 <span class="identifier">addable</span><span class="special"><>,</span> 44 <span class="identifier">ostreamable</span><span class="special"><></span> 45 <span class="special">></span> 46<span class="special">></span> <span class="identifier">any_type</span><span class="special">;</span> 47<span class="identifier">any_type</span> <span class="identifier">x</span><span class="special">(</span><span class="number">10</span><span class="special">);</span> 48<span class="identifier">any_type</span> <span class="identifier">y</span><span class="special">(</span><span class="number">7</span><span class="special">);</span> 49<span class="identifier">any_type</span> <span class="identifier">z</span><span class="special">(</span><span class="identifier">x</span> <span class="special">+</span> <span class="identifier">y</span><span class="special">);</span> 50<span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</span> <span class="identifier">z</span> <span class="special"><<</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span> <span class="comment">// prints 17</span> 51</pre> 52<p> 53 </p> 54<p> 55 This is <span class="emphasis"><em>not</em></span> a multimethod. The underlying types of the 56 arguments of <code class="computeroutput"><span class="special">+</span></code> must be the same 57 or the behavior is undefined. This example is correct because the arguments 58 both hold <code class="computeroutput"><span class="keyword">int</span></code>'s. 59 </p> 60<div class="note"><table border="0" summary="Note"> 61<tr> 62<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../doc/src/images/note.png"></td> 63<th align="left">Note</th> 64</tr> 65<tr><td align="left" valign="top"><p> 66 Adding <code class="computeroutput"><a class="link" href="../boost/type_erasure/relaxed.html" title="Struct relaxed">relaxed</a></code> 67 leads an exception rather than undefined behavior if the argument types are 68 wrong. 69 </p></td></tr> 70</table></div> 71<p> 72 <code class="computeroutput"><a class="link" href="../boost/type_erasure/addable.html" title="Struct template addable">addable</a></code><code class="computeroutput"><span class="special"><></span></code> requires the types of the arguments 73 to be exactly the same. This doesn't cover all uses of addition though. For 74 example, pointer arithmetic takes a pointer and an integer and returns a pointer. 75 We can capture this kind of relationship among several types by identifying 76 each type involved with a placeholder. We'll let the placeholder <code class="computeroutput"><span class="identifier">_a</span></code> represent the pointer and the placeholder 77 <code class="computeroutput"><span class="identifier">_b</span></code> represent the integer. 78 </p> 79<p> 80</p> 81<pre class="programlisting"><span class="keyword">int</span> <span class="identifier">array</span><span class="special">[</span><span class="number">5</span><span class="special">];</span> 82 83<span class="keyword">typedef</span> <span class="identifier">mpl</span><span class="special">::</span><span class="identifier">vector</span><span class="special"><</span> 84 <span class="identifier">copy_constructible</span><span class="special"><</span><span class="identifier">_a</span><span class="special">>,</span> 85 <span class="identifier">copy_constructible</span><span class="special"><</span><span class="identifier">_b</span><span class="special">>,</span> 86 <span class="identifier">typeid_</span><span class="special"><</span><span class="identifier">_a</span><span class="special">>,</span> 87 <span class="identifier">addable</span><span class="special"><</span><span class="identifier">_a</span><span class="special">,</span> <span class="identifier">_b</span><span class="special">,</span> <span class="identifier">_a</span><span class="special">></span> 88<span class="special">></span> <span class="identifier">requirements</span><span class="special">;</span> 89</pre> 90<p> 91 </p> 92<p> 93 Our new concept, <code class="computeroutput"><span class="identifier">addable</span><span class="special"><</span><span class="identifier">_a</span><span class="special">,</span> <span class="identifier">_b</span><span class="special">,</span> <span class="identifier">_a</span><span class="special">></span></code> 94 captures the rules of pointer addition: <code class="computeroutput"><span class="identifier">_a</span> 95 <span class="special">+</span> <span class="identifier">_b</span> <span class="special">-></span> <span class="identifier">_a</span></code>. 96 </p> 97<p> 98 Also, we can no longer capture the variables independently. 99</p> 100<pre class="programlisting"><span class="identifier">any</span><span class="special"><</span><span class="identifier">requirements</span><span class="special">,</span> <span class="identifier">_a</span><span class="special">></span> <span class="identifier">ptr</span><span class="special">(&</span><span class="identifier">array</span><span class="special">[</span><span class="number">0</span><span class="special">]);</span> <span class="comment">// illegal</span> 101</pre> 102<p> 103 This doesn't work because the library needs to know the type that _b binds 104 to when it captures the concept bindings. We need to specify the bindings of 105 both placeholders when we construct the <code class="computeroutput"><a class="link" href="../boost/type_erasure/any.html" title="Class template any">any</a></code>. 106 </p> 107<p> 108</p> 109<pre class="programlisting"><span class="keyword">typedef</span> <span class="identifier">mpl</span><span class="special">::</span><span class="identifier">map</span><span class="special"><</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">pair</span><span class="special"><</span><span class="identifier">_a</span><span class="special">,</span> <span class="keyword">int</span><span class="special">*>,</span> <span class="identifier">mpl</span><span class="special">::</span><span class="identifier">pair</span><span class="special"><</span><span class="identifier">_b</span><span class="special">,</span> <span class="keyword">int</span><span class="special">></span> <span class="special">></span> <span class="identifier">types</span><span class="special">;</span> 110<span class="identifier">any</span><span class="special"><</span><span class="identifier">requirements</span><span class="special">,</span> <span class="identifier">_a</span><span class="special">></span> <span class="identifier">ptr</span><span class="special">(&</span><span class="identifier">array</span><span class="special">[</span><span class="number">0</span><span class="special">],</span> <span class="identifier">make_binding</span><span class="special"><</span><span class="identifier">types</span><span class="special">>());</span> 111<span class="identifier">any</span><span class="special"><</span><span class="identifier">requirements</span><span class="special">,</span> <span class="identifier">_b</span><span class="special">></span> <span class="identifier">idx</span><span class="special">(</span><span class="number">2</span><span class="special">,</span> <span class="identifier">make_binding</span><span class="special"><</span><span class="identifier">types</span><span class="special">>());</span> 112<span class="identifier">any</span><span class="special"><</span><span class="identifier">requirements</span><span class="special">,</span> <span class="identifier">_a</span><span class="special">></span> <span class="identifier">x</span><span class="special">(</span><span class="identifier">ptr</span> <span class="special">+</span> <span class="identifier">idx</span><span class="special">);</span> 113<span class="comment">// x now holds array + 2</span> 114</pre> 115<p> 116 </p> 117<p> 118 Now that the arguments of <code class="computeroutput"><span class="special">+</span></code> aren't 119 the same type, we require that both arguments agree that <code class="computeroutput"><span class="identifier">_a</span></code> 120 maps to <code class="computeroutput"><span class="keyword">int</span><span class="special">*</span></code> 121 and that <code class="computeroutput"><span class="identifier">_b</span></code> maps to <code class="computeroutput"><span class="keyword">int</span></code>. 122 </p> 123<p> 124 We can also use <code class="computeroutput"><a class="link" href="../boost/type_erasure/tuple.html" title="Class template tuple">tuple</a></code> 125 to avoid having to write out the map out explicitly. <code class="computeroutput"><a class="link" href="../boost/type_erasure/tuple.html" title="Class template tuple">tuple</a></code> 126 is just a convenience class that combines the placeholder bindings it gets 127 from all its arguments. 128 </p> 129<p> 130</p> 131<pre class="programlisting"><span class="identifier">tuple</span><span class="special"><</span><span class="identifier">requirements</span><span class="special">,</span> <span class="identifier">_a</span><span class="special">,</span> <span class="identifier">_b</span><span class="special">></span> <span class="identifier">t</span><span class="special">(&</span><span class="identifier">array</span><span class="special">[</span><span class="number">0</span><span class="special">],</span> <span class="number">2</span><span class="special">);</span> 132<span class="identifier">any</span><span class="special"><</span><span class="identifier">requirements</span><span class="special">,</span> <span class="identifier">_a</span><span class="special">></span> <span class="identifier">y</span><span class="special">(</span><span class="identifier">get</span><span class="special"><</span><span class="number">0</span><span class="special">>(</span><span class="identifier">t</span><span class="special">)</span> <span class="special">+</span> <span class="identifier">get</span><span class="special"><</span><span class="number">1</span><span class="special">>(</span><span class="identifier">t</span><span class="special">));</span> 133</pre> 134<p> 135 </p> 136</div> 137<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> 138<td align="left"></td> 139<td align="right"><div class="copyright-footer">Copyright © 2011-2013 Steven Watanabe<p> 140 Distributed under the Boost Software License, Version 1.0. (See accompanying 141 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>) 142 </p> 143</div></td> 144</tr></table> 145<hr> 146<div class="spirit-nav"> 147<a accesskey="p" href="composing_concepts.html"><img src="../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../boost_typeerasure.html"><img src="../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="concept.html"><img src="../../../doc/src/images/next.png" alt="Next"></a> 148</div> 149</body> 150</html> 151