blob: c0ce51087c55cb2677521ca24343f395374a4a49 [file] [log] [blame]
Jeff Thompsona28eed82013-08-22 16:21:10 -07001<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
2
3<html>
4<head>
5 <meta http-equiv="Content-Language" content="en-us">
6 <meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
7
8 <title>Boost Function Object Adapter Library</title>
9</head>
10
11<body bgcolor="#FFFFFF" text="#000000">
12 <table border="1" bgcolor="#007F7F" cellpadding="2" summary="">
13 <tr>
14 <td bgcolor="#FFFFFF"><img src="../../boost.png" alt=
15 "boost.png (6897 bytes)" width="277" height="86"></td>
16
17 <td><a href="../../index.htm"><font face="Arial" color=
18 "#FFFFFF"><big>Home</big></font></a></td>
19
20 <td><a href="../libraries.htm"><font face="Arial" color=
21 "#FFFFFF"><big>Libraries</big></font></a></td>
22
23 <td><a href="http://www.boost.org/people/people.htm"><font face="Arial" color=
24 "#FFFFFF"><big>People</big></font></a></td>
25
26 <td><a href="http://www.boost.org/more/faq.htm"><font face="Arial" color=
27 "#FFFFFF"><big>FAQ</big></font></a></td>
28
29 <td><a href="../../more/index.htm"><font face="Arial" color=
30 "#FFFFFF"><big>More</big></font></a></td>
31 </tr>
32 </table>
33
34 <h1>Function Object Traits</h1>
35
36 <p>The header <a href="../../boost/functional.hpp">functional.hpp</a>
37 provides two traits class templates for functions and function objects:</p>
38
39 <table border="1" summary="">
40 <tr>
41 <th>Type</th>
42
43 <th>Contents</th>
44
45 <th>Description</th>
46 </tr>
47
48 <tr>
49 <td valign="top" rowspan="4">
50 <tt>template&nbsp;&lt;typename&nbsp;T&gt;<br>
51 struct&nbsp;unary_traits</tt></td>
52
53 <td valign="top"><tt>function_type</tt></td>
54
55 <td valign="top">The type of the function or function object itself
56 (i.e., <tt>T</tt>).</td>
57 </tr>
58
59 <tr>
60 <td valign="top"><tt>param_type</tt></td>
61
62 <td valign="top">The type that should be used to pass the function or
63 function object as a parameter.</td>
64 </tr>
65
66 <tr>
67 <td valign="top"><tt>result_type</tt></td>
68
69 <td valign="top">The type returned by the function or function
70 object.</td>
71 </tr>
72
73 <tr>
74 <td valign="top"><tt>argument_type</tt></td>
75
76 <td valign="top">The type of the argument to the function or function
77 object.</td>
78 </tr>
79
80 <tr>
81 <td valign="top" rowspan="5">
82 <tt>template&nbsp;&lt;typename&nbsp;T&gt;<br>
83 struct&nbsp;binary_traits</tt></td>
84
85 <td valign="top"><tt>function_type</tt></td>
86
87 <td valign="top">The type of the function or function object itself
88 (i.e., <tt>T</tt>).</td>
89 </tr>
90
91 <tr>
92 <td valign="top"><tt>param_type</tt></td>
93
94 <td valign="top">The type that should be used to pass the function or
95 function object as a parameter.</td>
96 </tr>
97
98 <tr>
99 <td valign="top"><tt>result_type</tt></td>
100
101 <td valign="top">The type returned by the function or function
102 object.</td>
103 </tr>
104
105 <tr>
106 <td valign="top"><tt>first_argument_type</tt></td>
107
108 <td valign="top">The type of the first argument to the function or
109 function object.</td>
110 </tr>
111
112 <tr>
113 <td valign="top"><tt>second_argument_type</tt></td>
114
115 <td valign="top">The type of the second argument to the function or
116 function object.</td>
117 </tr>
118 </table>
119
120 <h3>Usage</h3>
121
122 <p><tt>unary_traits</tt> should be instantiated with either a function
123 taking a single parameter, or an adaptable unary function object (i.e., a
124 class derived from <tt>std::unary_function</tt> or one which provides the
125 same typedefs). (See &sect;20.3.1 in the C++ Standard.)</p>
126
127 <p><tt>binary_traits</tt> should be instantiated with either a function
128 taking two parameters, or an adaptable binary function object (i.e., a
129 class derived from <tt>std::binary_function</tt> or one which provides the
130 same typedefs). (See &sect;20.3.1 in the C++ Standard.)</p>
131
132 <p>The most common usage of these templates is in function object adapters,
133 thus allowing them to adapt plain functions as well as function objects.
134 You can do this by wherever you would normally write, for example,</p>
135
136 <blockquote>
137 <pre>
138typename Operation::argument_type
139</pre>
140 </blockquote>
141
142 <p>simply writing</p>
143
144 <blockquote>
145 <pre>
146typename boost::unary_traits&lt;Operation&gt;::argument_type
147</pre>
148 </blockquote>
149
150 <p>instead.</p>
151
152 <h3>Additional Types Defined</h3>
153
154 <p>In addition to the standard result and argument typedefs, these traits
155 templates define two additional types.</p>
156
157 <h4><tt>function_type</tt></h4>
158
159 <p>This is the type of the function or function object, and can be used in
160 declarations such as</p>
161
162 <blockquote>
163 <pre>
164template &lt;class Predicate&gt;
165class unary_negate : // ...
166{
167 // ...
168 private:
169 <strong>typename unary_traits&lt;Predicate&gt;::function_type</strong> pred;
170};
171</pre>
172 </blockquote>
173
174 <p>If this typedef were not provided, it would not be possible to declare
175 <tt>pred</tt> in a way that would allow <tt>unary_negate</tt> to be
176 instantiated with a function type (see the C++ Standard &sect;14.3.1
177 &para;3).</p>
178
179 <h4><tt>param_type</tt></h4>
180
181 <p>This is a type suitable for passing the function or function object as a
182 parameter to another function. For example,</p>
183
184 <blockquote>
185 <pre>
186template &lt;class Predicate&gt;
187class unary_negate : // ...
188{
189 public:
190 explicit unary_negate(<strong>typename unary_traits&lt;Predicate&gt;::param_type</strong> x)
191 :
192 pred(x)
193 {}
194 // ...
195};
196</pre>
197 </blockquote>
198
199 <p>Function objects are passed by reference to const; function pointers are
200 passed by value.</p>
201
202 <h3>Limitations</h3>
203
204 <p>This library uses these traits within all function object adapters,
205 theoretically rendering <tt>ptr_fun</tt> obsolete. However, third party
206 adapters probably won't take advantage of this mechanism, and so
207 <tt>ptr_fun</tt> may still be required. Accordingly, this library also
208 provides <a href="ptr_fun.html">improved versions of the standard function
209 pointer adapters</a>.</p>
210
211 <p>These traits templates will also not work with compilers that fail to
212 support partial specialisation of templates. With these compilers, the
213 traits templates can only be instantiated with adaptable function objects,
214 thus requiring <tt>ptr_fun</tt> to be used, even with the function object
215 adapters in this library.</p>
216 <hr>
217
218 <p><a href="http://validator.w3.org/check?uri=referer"><img border="0" src=
219 "../../doc/images/valid-html401.png" alt="Valid HTML 4.01 Transitional"
220 height="31" width="88"></a></p>
221
222 <p>Revised
223 <!--webbot bot="Timestamp" s-type="EDITED" s-format="%d %B, %Y" startspan -->02
224 December, 2006<!--webbot bot="Timestamp" endspan i-checksum="38510" --></p>
225
226 <p><i>Copyright &copy; 2000 Cadenza New Zealand Ltd.</i></p>
227
228 <p><i>Distributed under the Boost Software License, Version 1.0. (See
229 accompanying file <a href="../../LICENSE_1_0.txt">LICENSE_1_0.txt</a> or
230 copy at <a href=
231 "http://www.boost.org/LICENSE_1_0.txt">http://www.boost.org/LICENSE_1_0.txt</a>)</i></p>
232</body>
233</html>