negators.html 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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>Negators</h1>
  26. <p>The header <a href="../../boost/functional.hpp">functional.hpp</a>
  27. provides enhanced versions of both the negator adapters from the C++
  28. Standard Library (&sect;20.3.5):</p>
  29. <ul>
  30. <li><tt>unary_negate</tt></li>
  31. <li><tt>binary_negate</tt></li>
  32. </ul>
  33. <p>As well as the corresponding helper functions</p>
  34. <ul>
  35. <li><tt>not1</tt></li>
  36. <li><tt>not2</tt></li>
  37. </ul>
  38. <p>However, the negators in this library improve on the standard versions
  39. in two ways:</p>
  40. <ul>
  41. <li>They use <a href="function_traits.html">function object traits</a> to
  42. avoid the need for <tt>ptr_fun</tt> when negating a function rather than
  43. an adaptable function object.</li>
  44. <li>They use Boost <a href=
  45. "../utility/call_traits.htm">call&nbsp;traits</a> to determine the best
  46. way to declare their arguments and pass them through to the adapted
  47. function (see <a href="#arguments">below</a>).</li>
  48. </ul>
  49. <h3>Usage</h3>
  50. <p>Usage is identical to the standard negators. For example,</p>
  51. <blockquote>
  52. <pre>
  53. bool bad(const Foo &amp;foo) { ... }
  54. ...
  55. std::vector&lt;Foo&gt; c;
  56. ...
  57. std::find_if(c.begin(), c.end(), boost::not1(bad));
  58. </pre>
  59. </blockquote>
  60. <h3 id="arguments">Argument Types</h3>
  61. <p>The C++ Standard (&sect;20.3.5) defines unary negate like this (binary
  62. negate is similar):</p>
  63. <blockquote>
  64. <pre>
  65. template &lt;class Predicate&gt;
  66. class unary_negate
  67. : public unary_function&lt;typename Predicate::argument_type,bool&gt; {
  68. public:
  69. explicit unary_negate(const Predicate&amp; pred);
  70. bool operator()(<strong>const typename Predicate::argument_type&amp;</strong> x) const;
  71. };
  72. </pre>
  73. </blockquote>
  74. <p>Note that if the Predicate's <tt>argument_type</tt> is a reference, the
  75. type of <tt>operator()</tt>'s argument would be a reference to a reference.
  76. Currently this is illegal in C++ (but see the <a href=
  77. "http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#106">C++
  78. standard core language active issues list</a>).</p>
  79. <p>However, if we instead defined <tt>operator()</tt> to accept Predicate's
  80. argument_type unmodified, this would be needlessly inefficient if it were a
  81. value type; the argument would be copied twice - once when calling
  82. <tt>unary_negate</tt>'s <tt>operator()</tt>, and again when
  83. <tt>operator()</tt> called the adapted function.</p>
  84. <p>So how we want to declare the argument for <tt>operator()</tt> depends
  85. on whether or not the Predicate's <tt>argument_type</tt> is a reference. If
  86. it is a reference, we want to declare it simply as <tt>argument_type</tt>;
  87. if it is a value we want to declare it as
  88. <tt>const&nbsp;argument_type&amp;</tt>.</p>
  89. <p>The Boost <a href="../utility/call_traits.htm">call_traits</a> class
  90. template contains a <tt>param_type</tt> typedef, which uses partial
  91. specialisation to make precisely this decision. If we were to declare
  92. <tt>operator()</tt> as</p>
  93. <blockquote>
  94. <pre>
  95. bool operator()(typename call_traits&lt;typename Predicate::argument_type&gt;::param_type x) const
  96. </pre>
  97. </blockquote>
  98. <p>the desired result would be achieved - we would eliminate references to
  99. references without loss of efficiency. In fact, the actual declaration is
  100. slightly more complicated because of the use of function object traits, but
  101. the effect remains the same.</p>
  102. <h3>Limitations</h3>
  103. <p>Both the function object traits and call traits used to realise these
  104. improvements rely on partial specialisation, these improvements are only
  105. available on compilers that support that feature. With other compilers, the
  106. negators in this library behave very much like those in the Standard -
  107. <tt>ptr_fun</tt> will be required to adapt functions, and references to
  108. references will not be avoided.</p>
  109. <hr>
  110. <p><a href="http://validator.w3.org/check?uri=referer"><img border="0" src=
  111. "../../doc/images/valid-html401.png" alt="Valid HTML 4.01 Transitional"
  112. height="31" width="88"></a></p>
  113. <p>Revised
  114. <!--webbot bot="Timestamp" s-type="EDITED" s-format="%d %B, %Y" startspan -->02
  115. December, 2006<!--webbot bot="Timestamp" endspan i-checksum="38510" --></p>
  116. <p><i>Copyright &copy; 2000 Cadenza New Zealand Ltd.</i></p>
  117. <p><i>Distributed under the Boost Software License, Version 1.0. (See
  118. accompanying file <a href="../../LICENSE_1_0.txt">LICENSE_1_0.txt</a> or
  119. copy at <a href=
  120. "http://www.boost.org/LICENSE_1_0.txt">http://www.boost.org/LICENSE_1_0.txt</a>)</i></p>
  121. </body>
  122. </html>