<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 631: conflicting requirements for BinaryPredicate</title>
<meta property="og:title" content="Issue 631: conflicting requirements for BinaryPredicate">
<meta property="og:description" content="C++ library issue. Status: NAD">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue631.html">
<meta property="og:type" content="website">
<meta property="og:image" content="http://cplusplus.github.io/LWG/images/cpp_logo.png">
<meta property="og:image:alt" content="C++ logo">
<style>
  p {text-align:justify}
  li {text-align:justify}
  pre code.backtick::before { content: "`" }
  pre code.backtick::after { content: "`" }
  blockquote.note
  {
    background-color:#E0E0E0;
    padding-left: 15px;
    padding-right: 15px;
    padding-top: 1px;
    padding-bottom: 1px;
  }
  ins {background-color:#A0FFA0}
  del {background-color:#FFA0A0}
  table.issues-index { border: 1px solid; border-collapse: collapse; }
  table.issues-index th { text-align: center; padding: 4px; border: 1px solid; }
  table.issues-index td { padding: 4px; border: 1px solid; }
  table.issues-index td:nth-child(1) { text-align: right; }
  table.issues-index td:nth-child(2) { text-align: left; }
  table.issues-index td:nth-child(3) { text-align: left; }
  table.issues-index td:nth-child(4) { text-align: left; }
  table.issues-index td:nth-child(5) { text-align: center; }
  table.issues-index td:nth-child(6) { text-align: center; }
  table.issues-index td:nth-child(7) { text-align: left; }
  table.issues-index td:nth-child(5) span.no-pr { color: red; }
  @media (prefers-color-scheme: dark) {
     html {
        color: #ddd;
        background-color: black;
     }
     ins {
        background-color: #225522
     }
     del {
        background-color: #662222
     }
     a {
        color: #6af
     }
     a:visited {
        color: #6af
     }
     blockquote.note
     {
        background-color: rgba(255, 255, 255, .10)
     }
  }
</style>
</head>
<body>
<hr>
<p><em>This page is a snapshot from the LWG issues list, see the <a href="lwg-active.html">Library Active Issues List</a> for more information and the meaning of <a href="lwg-active.html#NAD">NAD</a> status.</em></p>
<h3 id="631"><a href="lwg-closed.html#631">631</a>. conflicting requirements for <code>BinaryPredicate</code></h3>
<p><b>Section:</b> 26 <a href="https://wg21.link/algorithms">[algorithms]</a> <b>Status:</b> <a href="lwg-active.html#NAD">NAD</a>
 <b>Submitter:</b> James Kanze <b>Opened:</b> 2007-01-31 <b>Last modified:</b> 2016-01-28</p>
<p><b>Priority: </b>Not Prioritized
</p>
<p><b>View other</b> <a href="lwg-index-open.html#algorithms">active issues</a> in [algorithms].</p>
<p><b>View all other</b> <a href="lwg-index.html#algorithms">issues</a> in [algorithms].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#NAD">NAD</a> status.</p>
<p><b>Discussion:</b></p>
<p>
The general requirements for <code><code>BinaryPredicate</code></code> (in 26 <a href="https://wg21.link/algorithms">[algorithms]</a>/8) contradict the implied specific requirements for
some functions. In particular, it says that:
</p>

<blockquote><p>
[...] if an algorithm takes <code>BinaryPredicate <i>binary_pred</i></code>
as its argument and <code><i>first1</i></code> and <i>first2</i> as its
iterator arguments, it should work correctly in the construct <code>if
(binary_pred (*<i>first1</i> , *<i>first2</i> )){...}</code>.
<code>BinaryPredicate</code> always takes the first iterator type as its
first argument, that is, in those cases when <code>T <i>value</i></code> is
part of the signature, it should work correctly in the context of <code>if
(binary_pred (*<i>first1</i> , <i>value</i>)){...}</code>.
</p></blockquote>

<p>
In the description of <code>upper_bound</code> (26.8.4.3 <a href="https://wg21.link/upper.bound">[upper.bound]</a>/2), however, the use is described as
"<code>!comp(<i>value</i>, <i>e</i>)</code>", where <code><i>e</i></code> is an
element of the sequence (a result of dereferencing
<code>*<i>first</i></code>).
</p>

<p>
In the description of <code>lexicographical_compare</code>, we have both
"<code>*<i>first1</i> &lt; *<i>first2</i></code>" and "<code>*<i>first2</i>
&lt; *<i>first1</i></code>" (which presumably implies "<code>comp(
*<i>first1</i>, *<i>first2</i> )</code>" and "<code>comp( *<i>first2</i>,
*<i>first1</i> )</code>".
</p>

<p>
Logically, the <code>BinaryPredicate</code> is used as an ordering
relationship, with the semantics of "less than".  Depending on the
function, it may be used to determine equality, or any of the inequality
relationships; doing this requires being able to use it with either
parameter first.  I would thus suggest that the requirement be:
</p>

<p>
Alternatively, one could specify an order for each function. IMHO, this
would be more work for the committee, more work for the implementors,
and of no real advantage for the user: some functions, such as
<code>lexicographical_compare</code> or <code>equal_range</code>, will still require both
functions, and it seems like a much easier rule to teach that both
functions are always required, rather than to have a complicated list of
when you only need one, and which one.
</p>

<p><i>[
Toronto:  Moved to Open.  ConceptGCC seems to get <code>lower_bound</code>
and <code>upper_bound</code> to work withoutt these changes.
]</i></p>


<p><i>[
2009-07-28 Reopened by Alisdair.  No longer solved by concepts.
]</i></p>


<p><i>[
2009-10 Santa Cruz:
]</i></p>


<blockquote><p>
Move to Review. The small problem with the "iterator type"
will be fixed. The cited functions (<code>lower_bound</code>, <code>uppwer_bound</code>,
<code>equal_range</code>) don't actually use <code>BinaryPredicate</code> , and where it is used,
it is consistent with  [algorithm]/8, so the main complaint of the issue
is moot.
</p></blockquote>

<p><i>[
2010-01-16 Beman clarified wording.
]</i></p>


<p><i>[
2010-01-31: Moved to Tentatively NAD after 5 positive votes on c++std-lib. 
Rationale added below.
]</i></p>




<p><b>Rationale:</b></p>
<p><i>[
post San Francisco:
]</i></p>


<blockquote>
<p>
Solved by
<a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2759.pdf">N2759</a>.
</p>
</blockquote>

<p>
2010-01-31: The draft standard is well specified as is, and this specification
is desired.  Issues <a href="lwg-defects.html#556" title="Is Compare a BinaryPredicate? (Status: C++11)">556</a><sup><a href="https://cplusplus.github.io/LWG/issue556" title="Latest snapshot">(i)</a></sup> and <a href="lwg-defects.html#870" title="Do unordered containers not support function pointers for predicate&#47;hasher? (Status: C++11)">870</a><sup><a href="https://cplusplus.github.io/LWG/issue870" title="Latest snapshot">(i)</a></sup> solve the remaining
unclearness regarding the meaning of BinaryPredicate.
</p>



<p id="res-631"><b>Proposed resolution:</b></p>
<p><i>Change 26 <a href="https://wg21.link/algorithms">[algorithms]</a> paragraph 8 as indicated:</i></p>

<blockquote>

<p>
8 The <code>BinaryPredicate</code> parameter is used whenever an algorithm expects a
function object that when applied to the result of dereferencing two
corresponding iterators or to dereferencing an iterator and type <code>T</code> when
<code>T</code> is part of the signature returns a value testable as true. <ins>
<code>BinaryPredicate</code> always takes the first iterator <code>value_type</code> as
one of its arguments; which argument is unspecified.</ins> <del>In other words,
if</del> <ins> If</ins> an algorithm takes <code>BinaryPredicate binary_pred</code>
as its argument and <code>first1</code> and <code>first2</code> as its iterator
arguments, it should work correctly <ins>both</ins> in the construct <code>if
(binary_pred(*first1, *first2)){...}</code> <ins>and <code>if (binary_pred (*first2,
*first1)){...}</code></ins>. <del><code>BinaryPredicate</code> always takes the first
iterator type as its first argument, that is, in</del> <ins>In</ins> those cases
when <code>T value</code> is part of the signature, it should work correctly in the
context of <code> if (binary_pred(*first1, value)){...}</code> <ins>and of <code>if
(binary_pred (value, *first1)){...}</code></ins>. <del> <code>binary_pred</code> shall
not apply any non-constant function through the dereferenced iterators.</del>
<ins>[<i>Note:</i> if the two types are not identical, and neither is
convertable to the other, this may require that the <code>BinaryPredicate</code> be
a functional object with two overloaded <code>operator()()</code> functions.
&mdash; <i>end note</i>]</ins>
</p>

</blockquote>






</body>
</html>
