<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 1205: Some algorithms could more clearly document their handling of empty ranges</title>
<meta property="og:title" content="Issue 1205: Some algorithms could more clearly document their handling of empty ranges">
<meta property="og:description" content="C++ library issue. Status: C++11">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue1205.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#C++11">C++11</a> status.</em></p>
<h3 id="1205"><a href="lwg-defects.html#1205">1205</a>. Some algorithms could more clearly document their handling of empty ranges</h3>
<p><b>Section:</b> 26 <a href="https://wg21.link/algorithms">[algorithms]</a> <b>Status:</b> <a href="lwg-active.html#C++11">C++11</a>
 <b>Submitter:</b> Alisdair Meredith <b>Opened:</b> 2009-09-13 <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#C++11">C++11</a> status.</p>
<p><b>Discussion:</b></p>
<p>
There are a number of algorithms whose result might depend on the
handling of an empty range.  In some cases the result is not clear,
while in others it would help readers to clearly mention the result
rather than require some subtle intuition of the supplied wording.
</p>

<p>
 [alg.all_of]
</p>

<blockquote><p>
<i>Returns:</i> <code>true</code> if <code>pred(*i)</code> is <code>true</code> for every
iterator <code>i</code> in the range <code>[first,last)</code>, ...
</p></blockquote>

<p>
What does this mean if the range is empty?
</p>

<p>
I believe that we intend this to be <code>true</code> and suggest a
non-normative note to clarify:
</p>

<p>
Add to p1  [alg.all_of]:
</p>

<blockquote><p>
[<i>Note:</i> Returns <code>true</code> if <code>[first,last)</code> is empty. 
&mdash; <i>end note</i>]
</p></blockquote>

<p>
 [alg.none_of]
</p>

<blockquote><p>
<i>Returns:</i> <code>true</code> if <code>pred(*i)</code> is <code>false</code> for every
iterator <code>i</code> in the range <code>[first,last)</code>, ...
</p></blockquote>

<p>
What does this mean if the range empty?
</p>

<p>
I believe that we intend this to be <code>true</code> and suggest a
non-normative note to clarify:
</p>

<p>
Add to p1  [alg.none_of]:
</p>

<blockquote><p>
[<i>Note:</i> Returns <code>true</code> if <code>[first,last)</code> is empty. 
&mdash; <i>end note</i>]
</p></blockquote>

<p>
 [alg.any_of]
</p>

<p>
The specification for an empty range is actually fairly clear in this
case, but a note wouldn't hurt and would be consistent with proposals
for <code>all_of</code>/<code>none_of</code> algorithms.
</p>

<p>
Add to p1  [alg.any_of]:
</p>

<blockquote><p>
[<i>Note:</i> Returns <code>false</code> if <code>[first,last)</code> is empty. 
&mdash; <i>end note</i>]
</p></blockquote>

<p>
26.6.8 <a href="https://wg21.link/alg.find.end">[alg.find.end]</a>
</p>

<p>
what does this mean if <code>[first2,last2)</code> is empty?
</p>

<p>
I believe the wording suggests the algorithm should return
<code>last1</code> in this case, but am not 100% sure. Is this in fact the
correct result anyway? Surely an empty range should always match and the
naive expected result would be <code>first1</code>?
</p>

<p>
My proposed wording is a note to clarify the current semantic:
</p>

<p>
Add to p2 26.6.8 <a href="https://wg21.link/alg.find.end">[alg.find.end]</a>:
</p>

<blockquote><p>
[<i>Note:</i> Returns <code>last1</code> if <code>[first2,last2)</code> is
empty. &mdash; <i>end note</i>]
</p></blockquote>

<p>
I would prefer a normative wording treating empty ranges specially, but
do not believe we can change semantics at this point in the process,
unless existing implementations actually yield this result:
</p>

<p>
Alternative wording: (NOT a note)
</p>
<p>
Add to p2 26.6.8 <a href="https://wg21.link/alg.find.end">[alg.find.end]</a>:
</p>
<blockquote><p>
Returns <code>first1</code> if <code>[first2,last2)</code> is empty.
</p></blockquote>

<p>
26.6.9 <a href="https://wg21.link/alg.find.first.of">[alg.find.first.of]</a>
</p>

<p>
The phrasing seems precise when <code>[first2, last2)</code> is empty, but a small
note to confirm the reader's understanding might still help.
</p>

<p>
Add to p2 26.6.9 <a href="https://wg21.link/alg.find.first.of">[alg.find.first.of]</a>
</p>
<blockquote><p>
[<i>Note:</i> Returns <code>last1</code> if <code>[first2,last2)</code> is
empty. &mdash; <i>end note</i>]
</p></blockquote>

<p>
26.6.15 <a href="https://wg21.link/alg.search">[alg.search]</a>
</p>

<p>
What is the expected result if <code>[first2, last2)</code> is empty?
</p>

<p>
I believe the wording suggests the algorithm should return <code>last1</code> in this
case, but am not 100% sure. Is this in fact the correct result anyway? 
Surely an empty range should always match and the naive expected result
would be <code>first1</code>?
</p>

<p>
My proposed wording is a note to clarify the current semantic:
</p>

<p>
Add to p2 26.6.15 <a href="https://wg21.link/alg.search">[alg.search]</a>:
</p>

<blockquote><p>
[<i>Note:</i> Returns <code>last1</code> if <code>[first2,last2)</code> is
empty. &mdash; <i>end note</i>]
</p></blockquote>

<p>
Again, I would prefer a normative wording treating empty ranges
specially, but do not believe we can change semantics at this point in
the process, unless existing implementations actually yield this result:
</p>

<p>
Alternative wording: (NOT a note)
</p>
<p>
Add to p2 26.6.15 <a href="https://wg21.link/alg.search">[alg.search]</a>:
</p>

<blockquote><p>
Returns <code>first1</code> if <code>[first2,last2)</code> is empty.
</p></blockquote>

<p>
26.8.5 <a href="https://wg21.link/alg.partitions">[alg.partitions]</a>
</p>

<p>
Is an empty range partitioned or not?
</p>

<p>
Proposed wording:
</p>

<p>
Add to p1 26.8.5 <a href="https://wg21.link/alg.partitions">[alg.partitions]</a>:
</p>

<blockquote><p>
[<i>Note:</i> Returns <code>true</code> if <code>[first,last)</code> is empty. 
&mdash; <i>end note</i>]
</p></blockquote>

<p>
26.8.7.2 <a href="https://wg21.link/includes">[includes]</a>
</p>

<blockquote><p>
<i>Returns:</i> <code>true</code> if every element in the range
<code>[first2,last2)</code> is contained in the range
<code>[first1,last1)</code>. ...
</p></blockquote>

<p>
I really don't know what this means if <code>[first2,last2)</code> is empty.
I could loosely guess that this implies empty ranges always match, and
my proposed wording is to clarify exactly that:
</p>

<p>
Add to p1 26.8.7.2 <a href="https://wg21.link/includes">[includes]</a>:
</p>

<blockquote><p>
[<i>Note:</i> Returns <code>true</code> if <code>[first2,last2)</code> is empty.
&mdash; <i>end note</i>]
</p></blockquote>

<p>
26.8.8.3 <a href="https://wg21.link/pop.heap">[pop.heap]</a>
</p>

<p>
The effects clause is invalid if the range <code>[first,last)</code> is empty, unlike
all the other heap alogorithms.  The should be called out in the
requirements.
</p>

<p>
Proposed wording:
</p>
<p>
Revise p2 26.8.8.3 <a href="https://wg21.link/pop.heap">[pop.heap]</a>
</p>

<blockquote><p>
<i>Requires:</i> The range <code>[first,last)</code> shall be a valid
<ins>non-empty</ins> heap.
</p></blockquote>

<p>
[Editorial] Reverse order of 26.8.8.3 <a href="https://wg21.link/pop.heap">[pop.heap]</a> p1 and p2.
</p>

<p>
26.8.9 <a href="https://wg21.link/alg.min.max">[alg.min.max]</a>
</p>

<p>
<code>minmax_element</code> does not clearly specify behaviour for an empty
range in the same way that <code>min_element</code> and <code>max_element</code> do.
</p>

<p>
Add to p31 26.8.9 <a href="https://wg21.link/alg.min.max">[alg.min.max]</a>:
</p>

<blockquote><p>
Returns <code>make_pair(first, first)</code> if <code>first == last</code>.
</p></blockquote>

<p>
26.8.11 <a href="https://wg21.link/alg.lex.comparison">[alg.lex.comparison]</a>
</p>

<p>
The wording here seems quite clear, especially with the sample algorithm
implementation.  A note is recommended purely for consistency with the
rest of these issue resolutions:
</p>

<p>
Add to p1 26.8.11 <a href="https://wg21.link/alg.lex.comparison">[alg.lex.comparison]</a>:
</p>

<blockquote><p>
[<i>Note:</i> An empty sequence is lexicographically less than any other
non-empty sequence, but not to another empty sequence. &mdash; <i>end note</i>]
</p></blockquote>

<p><i>[
2009-11-11 Howard changes Notes to Remarks and changed <code>search</code> to
return <code>first1</code> instead of <code>last1</code>.
]</i></p>


<p><i>[
2009-11-11 Moved to Tentatively Ready after 5 positive votes on c++std-lib.
]</i></p>



<p id="res-1205"><b>Proposed resolution:</b></p>
<p>
Add to  [alg.all_of]:
</p>
<blockquote><p><ins>
<i>Remarks:</i> Returns <code>true</code> if <code>[first,last)</code> is empty. 
</ins></p></blockquote>

<p>
Add to  [alg.any_of]:
</p>

<blockquote><p><ins>
<i>Remarks:</i> Returns <code>false</code> if <code>[first,last)</code> is empty. 
</ins></p></blockquote>

<p>
Add to  [alg.none_of]:
</p>
<blockquote><p><ins>
<i>Remarks:</i> Returns <code>true</code> if <code>[first,last)</code> is empty. 
</ins></p></blockquote>

<p>
Add to 26.6.8 <a href="https://wg21.link/alg.find.end">[alg.find.end]</a>:
</p>
<blockquote><p><ins>
<i>Remarks:</i> Returns <code>last1</code> if <code>[first2,last2)</code> is
empty.
</ins></p></blockquote>

<p>
Add to 26.6.9 <a href="https://wg21.link/alg.find.first.of">[alg.find.first.of]</a>
</p>
<blockquote><p><ins>
<i>Remarks:</i> Returns <code>last1</code> if <code>[first2,last2)</code> is
empty.
</ins></p></blockquote>

<p>
Add to 26.6.15 <a href="https://wg21.link/alg.search">[alg.search]</a>:
</p>
<blockquote><p><ins>
<i>Remarks:</i> Returns <code>first1</code> if <code>[first2,last2)</code> is
empty.
</ins></p></blockquote>

<p>
Add to 26.8.5 <a href="https://wg21.link/alg.partitions">[alg.partitions]</a>:
</p>
<blockquote><p><ins>
<i>Remarks:</i> Returns <code>true</code> if <code>[first,last)</code> is empty. 
</ins></p></blockquote>

<p>
Add to 26.8.7.2 <a href="https://wg21.link/includes">[includes]</a>:
</p>
<blockquote><p><ins>
<i>Remarks:</i> Returns <code>true</code> if <code>[first2,last2)</code> is empty. 
</ins></p></blockquote>

<p>
Revise p2 26.8.8.3 <a href="https://wg21.link/pop.heap">[pop.heap]</a>
</p>
<blockquote><p>
<i>Requires:</i> The range <code>[first,last)</code> shall be a valid
<ins>non-empty</ins> heap.
</p></blockquote>

<p>
[Editorial] 
</p>
<blockquote><p>
Reverse order of 26.8.8.3 <a href="https://wg21.link/pop.heap">[pop.heap]</a> p1 and p2.
</p></blockquote>

<p>
Add to p35 26.8.9 <a href="https://wg21.link/alg.min.max">[alg.min.max]</a>:
</p>
<blockquote><pre>
template&lt;class ForwardIterator, class Compare&gt;
  pair&lt;ForwardIterator, ForwardIterator&gt;
    minmax_element(ForwardIterator first, ForwardIterator last, Compare comp);
</pre>
<blockquote><p>
<i>Returns:</i> <code>make_pair(m, M)</code>, where <code>m</code> is the first iterator in
<code>[first,last)</code> such that no iterator in the range refers to a smaller
element, and where <code>M</code> is the last iterator in <code>[first,last)</code> such that no
iterator in the range refers to a larger element. 
<ins>Returns <code>make_pair(first, first)</code> if <code>first == last</code>.</ins>
</p></blockquote>
</blockquote>

<p>
Add to 26.8.11 <a href="https://wg21.link/alg.lex.comparison">[alg.lex.comparison]</a>:
</p>
<blockquote><p><ins>
<i>Remarks:</i> An empty sequence is lexicographically less than any other
non-empty sequence, but not less than another empty sequence.
</ins></p></blockquote>






</body>
</html>
