[12] | 1 | <html> |
---|
| 2 | |
---|
| 3 | <head> |
---|
| 4 | <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> |
---|
| 5 | <title>Boost Function Object Adapter Library</title> |
---|
| 6 | </head> |
---|
| 7 | |
---|
| 8 | <body bgcolor="#FFFFFF" text="#000000"> |
---|
| 9 | |
---|
| 10 | <table border="1" bgcolor="#007F7F" cellpadding="2"> |
---|
| 11 | <tr> |
---|
| 12 | <td bgcolor="#FFFFFF"><img src="../../boost.png" alt="boost.png (6897 bytes)" WIDTH="277" HEIGHT="86"></td> |
---|
| 13 | <td><a href="../../index.htm"><font face="Arial" color="#FFFFFF"><big>Home </big></font></a></td> |
---|
| 14 | <td><a href="../libraries.htm"><font face="Arial" color="#FFFFFF"><big>Libraries </big></font></a></td> |
---|
| 15 | <td><a href="../../people/people.htm"><font face="Arial" color="#FFFFFF"><big>People </big></font></a></td> |
---|
| 16 | <td><a href="../../more/faq.htm"><font face="Arial" color="#FFFFFF"><big>FAQ </big></font></a></td> |
---|
| 17 | <td><a href="../../more/index.htm"><font face="Arial" color="#FFFFFF"><big>More </big></font></a></td> |
---|
| 18 | </tr> |
---|
| 19 | </table> |
---|
| 20 | |
---|
| 21 | <h1>Function Object Traits</h1> |
---|
| 22 | |
---|
| 23 | <p>The header <nobr><a |
---|
| 24 | href="../../boost/functional.hpp">functional.hpp</a></nobr> provides two |
---|
| 25 | traits class templates for functions and function objects:</p> |
---|
| 26 | |
---|
| 27 | <table border="1"> |
---|
| 28 | <tr> |
---|
| 29 | <th>Type</th> |
---|
| 30 | <th>Contents</th> |
---|
| 31 | <th>Description</th> |
---|
| 32 | </tr> |
---|
| 33 | <tr> |
---|
| 34 | <td valign="top" rowspan="4"><tt><nobr>template <typename T></nobr><br><nobr>struct unary_traits<nobr></tt> |
---|
| 35 | </td> |
---|
| 36 | <td valign="top"><tt><nobr>function_type</nobr></tt> |
---|
| 37 | </td> |
---|
| 38 | <td valign="top">The type of the function or function object itself (i.e., <tt>T</tt>). |
---|
| 39 | </td> |
---|
| 40 | </tr> |
---|
| 41 | <tr> |
---|
| 42 | <td valign="top"><tt><nobr>param_type</nobr></tt> |
---|
| 43 | </td> |
---|
| 44 | <td valign="top">The type that should be used to pass the function or function object as a parameter. |
---|
| 45 | </td> |
---|
| 46 | </tr> |
---|
| 47 | <tr> |
---|
| 48 | <td valign="top"><tt><nobr>result_type</nobr></tt> |
---|
| 49 | </td> |
---|
| 50 | <td valign="top">The type returned by the function or function object. |
---|
| 51 | </td> |
---|
| 52 | </tr> |
---|
| 53 | <tr> |
---|
| 54 | <td valign="top"><tt><nobr>argument_type</nobr></tt> |
---|
| 55 | </td> |
---|
| 56 | <td valign="top">The type of the argument to the function or function object. |
---|
| 57 | </td> |
---|
| 58 | </tr> |
---|
| 59 | <tr> |
---|
| 60 | <td valign="top" rowspan="5"><tt><nobr>template <typename T></nobr><br><nobr>struct binary_traits<nobr></tt> |
---|
| 61 | </td> |
---|
| 62 | <td valign="top"><tt><nobr>function_type</nobr></tt> |
---|
| 63 | </td> |
---|
| 64 | <td valign="top">The type of the function or function object itself (i.e., <tt>T</tt>). |
---|
| 65 | </td> |
---|
| 66 | </tr> |
---|
| 67 | <tr> |
---|
| 68 | <td valign="top"><tt><nobr>param_type</nobr></tt> |
---|
| 69 | </td> |
---|
| 70 | <td valign="top">The type that should be used to pass the function or function object as a parameter. |
---|
| 71 | </td> |
---|
| 72 | </tr> |
---|
| 73 | <tr> |
---|
| 74 | <td valign="top"><tt><nobr>result_type</nobr></tt> |
---|
| 75 | </td> |
---|
| 76 | <td valign="top">The type returned by the function or function object. |
---|
| 77 | </td> |
---|
| 78 | </tr> |
---|
| 79 | <tr> |
---|
| 80 | <td valign="top"><tt><nobr>first_argument_type</nobr></tt> |
---|
| 81 | </td> |
---|
| 82 | <td valign="top">The type of the first argument to the function or function object. |
---|
| 83 | </td> |
---|
| 84 | </tr> |
---|
| 85 | <tr> |
---|
| 86 | <td valign="top"><tt><nobr>second_argument_type</nobr></tt> |
---|
| 87 | </td> |
---|
| 88 | <td valign="top">The type of the second argument to the function or function object. |
---|
| 89 | </td> |
---|
| 90 | </tr> |
---|
| 91 | </table> |
---|
| 92 | |
---|
| 93 | <h3>Usage</h3> |
---|
| 94 | |
---|
| 95 | <p><tt><nobr>unary_traits</nobr></tt> should be instantiated with |
---|
| 96 | either a function taking a single parameter, or an adaptable unary |
---|
| 97 | function object (i.e., a class derived from |
---|
| 98 | <tt><nobr>std::unary_function</nobr></tt> or one which provides the |
---|
| 99 | same typedefs). (See §20.3.1 in the C++ Standard.) |
---|
| 100 | |
---|
| 101 | <p><tt><nobr>binary_traits</nobr></tt> should be instantiated with |
---|
| 102 | either a function taking two parameters, or an adaptable binary |
---|
| 103 | function object (i.e., a class derived from |
---|
| 104 | <tt><nobr>std::binary_function</nobr></tt> or one which provides the |
---|
| 105 | same typedefs). (See §20.3.1 in the C++ Standard.) |
---|
| 106 | |
---|
| 107 | <p>The most common usage of these templates is in function object |
---|
| 108 | adapters, thus allowing them to adapt plain functions as well as |
---|
| 109 | function objects. You can do this by wherever you would normally |
---|
| 110 | write, for example, |
---|
| 111 | |
---|
| 112 | <blockquote><pre> |
---|
| 113 | typename Operation::argument_type |
---|
| 114 | </pre></blockquote> |
---|
| 115 | |
---|
| 116 | <p>simply writing |
---|
| 117 | |
---|
| 118 | <blockquote><pre> |
---|
| 119 | typename boost::unary_traits<Operation>::argument_type |
---|
| 120 | </pre></blockquote> |
---|
| 121 | |
---|
| 122 | <p>instead. |
---|
| 123 | |
---|
| 124 | <h3>Additional Types Defined</h3> |
---|
| 125 | |
---|
| 126 | <p>In addition to the standard result and argument typedefs, these |
---|
| 127 | traits templates define two additional types. |
---|
| 128 | |
---|
| 129 | <h4><tt>function_type</tt></h4> |
---|
| 130 | |
---|
| 131 | <p>This is the type of the function or function object, and can be |
---|
| 132 | used in declarations such as</p> |
---|
| 133 | |
---|
| 134 | <blockquote><pre> |
---|
| 135 | template <class Predicate> |
---|
| 136 | class unary_negate : // ... |
---|
| 137 | { |
---|
| 138 | // ... |
---|
| 139 | private: |
---|
| 140 | <strong>typename unary_traits<Predicate>::function_type</strong> pred; |
---|
| 141 | }; |
---|
| 142 | </pre></blockquote> |
---|
| 143 | |
---|
| 144 | <p>If this typedef were not provided, it would not be possible to |
---|
| 145 | declare <tt>pred</tt> in a way that would allow |
---|
| 146 | <tt><nobr>unary_negate</nobr></tt> to be instantiated with a function |
---|
| 147 | type (see the C++ Standard §14.3.1 ¶3). |
---|
| 148 | |
---|
| 149 | <h4><tt>param_type</tt></h4> |
---|
| 150 | |
---|
| 151 | <p>This is a type suitable for passing the function or function object |
---|
| 152 | as a parameter to another function. For example, |
---|
| 153 | |
---|
| 154 | <blockquote><pre> |
---|
| 155 | template <class Predicate> |
---|
| 156 | class unary_negate : // ... |
---|
| 157 | { |
---|
| 158 | public: |
---|
| 159 | explicit unary_negate(<strong>typename unary_traits<Predicate>::param_type</strong> x) |
---|
| 160 | : |
---|
| 161 | pred(x) |
---|
| 162 | {} |
---|
| 163 | // ... |
---|
| 164 | }; |
---|
| 165 | </pre></blockquote> |
---|
| 166 | |
---|
| 167 | <p>Function objects are passed by reference to const; function |
---|
| 168 | pointers are passed by value.</p> |
---|
| 169 | |
---|
| 170 | |
---|
| 171 | <h3>Limitations</h3> |
---|
| 172 | |
---|
| 173 | <p>This library uses these traits within all function object adapters, |
---|
| 174 | theoretically rendering <tt><nobr>ptr_fun</nobr></tt> obsolete. |
---|
| 175 | However, third party adapters probably won't take advantage of this |
---|
| 176 | mechanism, and so <tt><nobr>ptr_fun</nobr></tt> may still be required. |
---|
| 177 | Accordingly, this library also provides <a |
---|
| 178 | href="ptr_fun.html">improved versions of the standard function pointer |
---|
| 179 | adapters</a>.</p> |
---|
| 180 | |
---|
| 181 | <p>These traits templates will also not work with compilers that fail |
---|
| 182 | to support partial specialisation of templates. With these compilers, |
---|
| 183 | the traits templates can only be instantiated with adaptable function |
---|
| 184 | objects, thus requiring <tt><nobr>ptr_fun</nobr></tt> to be used, even |
---|
| 185 | with the function object adapters in this library. |
---|
| 186 | |
---|
| 187 | <hr> |
---|
| 188 | <p>Copyright © 2000 Cadenza New Zealand Ltd. Permission to copy, |
---|
| 189 | use, modify, sell and distribute this document is granted provided |
---|
| 190 | this copyright notice appears in all copies. This document is provided |
---|
| 191 | "as is" without express or implied warranty, and with no claim as to |
---|
| 192 | its suitability for any purpose.</p> |
---|
| 193 | |
---|
| 194 | <p>Revised 28 June 2000</p> |
---|
| 195 | </body> |
---|
| 196 | </html> |
---|