function_traits.html 6.9 KB

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