<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 780: std::merge() specification incorrect&#47;insufficient</title>
<meta property="og:title" content="Issue 780: std::merge() specification incorrect&#47;insufficient">
<meta property="og:description" content="C++ library issue. Status: C++11">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue780.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="780"><a href="lwg-defects.html#780">780</a>. <code>std::merge()</code> specification incorrect&#47;insufficient</h3>
<p><b>Section:</b> 26.8.6 <a href="https://wg21.link/alg.merge">[alg.merge]</a> <b>Status:</b> <a href="lwg-active.html#C++11">C++11</a>
 <b>Submitter:</b> Daniel Kr&uuml;gler <b>Opened:</b> 2008-01-25 <b>Last modified:</b> 2016-01-28</p>
<p><b>Priority: </b>Not Prioritized
</p>
<p><b>View all other</b> <a href="lwg-index.html#alg.merge">issues</a> in [alg.merge].</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>
Though issue <a href="lwg-defects.html#283" title="std::replace() requirement incorrect/insufficient (Status: CD1)">283</a><sup><a href="https://cplusplus.github.io/LWG/issue283" title="Latest snapshot">(i)</a></sup> has fixed many open issues, it seems that some are still open:
</p>

<p>
Both 25.3.4 [lib.alg.merge] in 14882:2003 and 26.8.6 <a href="https://wg21.link/alg.merge">[alg.merge]</a> in N2461
have no Requires element and the Effects element contains some requirements,
which is probably editorial. Worse is that:
</p>

<ul>
<li>
no assignment requirements are specified (neither implicit nor explicit).
</li>

<li>
the effects clause just speaks of "merges", which is badly worded
near to a circular definition.
</li>

<li>
p. 2 mentions a range <code>[first, last)</code>, which is not defined by the
function arguments or otherwise.
</li>

<li>
p. 2 says "according to the ordering defined by comp" which is both
incomplete (because
this excludes the first variant with &lt;) and redundant (because the
following subordinate
clause mentions comp again)
</li>
</ul>

<p><i>[
Post Summit Alisdair adds:
]</i></p>


<blockquote>
<p>
Suggest:
</p>
<blockquote><p>
(where <code>last</code> is equal to <code>next(result, distance(first1, last1) +
distance(first2, last2))</code>, such that resulting range will be sorted in
non-decreasing order; that is, for every iterator <code>i</code> in <code>[result,last)</code> other
than <code>result</code>, the condition <code>*i &lt; *prev(i)</code> or, respectively, <code>comp(*i,
*prev(i))</code> will be <code>false</code>.
</p></blockquote>

<p>
Note that this might still not be technically accurate in the case of
<code>InputIterators</code>, depending on other resolutions working their way through the
system (<a href="lwg-defects.html#1011" title="next&#47;prev wrong iterator type (Status: C++11)">1011</a><sup><a href="https://cplusplus.github.io/LWG/issue1011" title="Latest snapshot">(i)</a></sup>).
</p>
</blockquote>

<p><i>[
Post Summit Daniel adds:
]</i></p>


<blockquote><p>
If we want to use <code>prev</code> and <code>next</code> here (Note: <code>merge</code>
is sufficiently satisfied with <code>InputIterator</code>) we should instead <em>add</em> more to
26 <a href="https://wg21.link/algorithms">[algorithms]</a> p. 6, but I can currently not propose any good wording for this.
</p></blockquote>

<p><i>[
Batavia (2009-05):
]</i></p>

<blockquote>
<p>
Pete points out the existing wording in [algorithms] p. 4
that permits the use of + in algorithm specifications.
</p>
<p>
Alisdair points out that that wording may not apply to input iterators.
</p>
<p>
Move to Review.
</p>
</blockquote>

<p><i>[
2009-07 Frankfurt:
]</i></p>


<blockquote><p>
Move to Ready.
</p></blockquote>

<p><i>[
2009-08-23 Daniel reopens:
]</i></p>


<blockquote>
<p>
The proposed wording must be rephrased, because the part
</p>

<blockquote><p>
for every iterator <code>i</code> in <code>[result,last)</code> other than <code>result</code>, the condition
<code>*i &lt; *(i - 1)</code> or, respectively, <code>comp(*i, *(i - 1))</code> will be <code>false</code>"
</p></blockquote>

<p>
isn't meaningful, because the range <code>[result,last)</code> is that of a pure
<code>OutputIterator</code>, which is not <em>readable</em> in general.
</p>

<p><i>[Howard:  Proposed wording updated by Daniel, status moved from Ready to Review.]</i></p>

</blockquote>

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


<blockquote>
<p>
Matt has some different words to propose.  Those words have been moved into
the proposed wording section, and the original proposed wording now appears
here:
</p>
<blockquote>
<p>
In 26.8.6 <a href="https://wg21.link/alg.merge">[alg.merge]</a> replace p.1+ 2:
</p>

<blockquote>
<p>
<i>Effects:</i> <del>Merges</del><ins>Copies all the elements of the</ins>
two sorted ranges
<code>[first1,last1)</code> and <code>[first2,last2)</code> into the range <code>[result,result +
(last1 - first1) + (last2 - first2))</code>
<ins>, such that resulting range will be sorted in non-decreasing
order; that is for every
pair of iterators <code>i</code> and <code>j</code> of either input ranges, where <code>*i</code> was copied
to the output range
before <code>*j</code> was copied to the output range, the condition <code>*j &lt; *i</code> or,
respectively, <code>comp(*j, *i)</code>
will be <code>false</code>.</ins>
</p>

<p>
<ins><i>Requires:</i></ins>The resulting range shall not overlap with either
of the original ranges.
<del>The list will be sorted in non-decreasing order according to the
ordering defined by
<code>comp</code>; that is, for every iterator <code>i</code> in <code>[first,last)</code> other than <code>first</code>,
the condition <code>*i &lt; *(i - 1)</code> or
<code>comp(*i, *(i - 1))</code> will be <code>false</code>.</del>
</p>
</blockquote>
</blockquote>
</blockquote>

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




<p id="res-780"><b>Proposed resolution:</b></p>

<p>
Change 26.8.6 <a href="https://wg21.link/alg.merge">[alg.merge]</a> 1 and 2:
</p>

<blockquote>
<p>1 <del>
<i>Effects:</i> Merges two sorted ranges <code>[first1,last1)</code> and
<code>[first2,last2)</code> into the range <code>[result, result + (last1 -
first1) + (last2 - first2))</code>.
</del></p>
<p><ins>
<i>Effects:</i> Copies all the elements of the two ranges
<code>[first1,last1)</code> and <code>[first2,last2)</code> into the range
<code>[result, result_last)</code>, where <code>result_last</code> is <code>result
+ (last1 - first1) + (last2 - first2)</code>, such that the resulting
range satisfies <code>is_sorted(result, result_last)</code> or
<code>is_sorted(result, result_last, comp)</code>, respectively.
</ins></p>

<p>
2 <ins><i>Requires:</i></ins> <ins>The ranges <code>[first1,last1)</code> and
<code>[first2,last2)</code> shall be sorted with respect to <code>operator&lt;</code> or
<code>comp</code>.</ins> The resulting range shall not overlap with either of the
original ranges.  <del>The list will be sorted in non-decreasing order according
to the ordering defined by <code>comp</code>; that is, for every iterator <code>i</code>
in <code>[first,last)</code> other than <code>first</code>, the condition <code>*i &lt;
*(i - 1)</code> or <code>comp(*i, *(i - 1))</code> will be <code>false</code>.</del>
</p>

</blockquote>

<p>
Change 26.8.6 <a href="https://wg21.link/alg.merge">[alg.merge]</a> p. 6+7 as indicated <i>[This ensures harmonization
between <code>inplace_merge</code> and <code>merge</code>]</i>
</p>

<blockquote>
<p>
6 <i>Effects:</i> Merges two <del>sorted</del> consecutive ranges
<code>[first,middle)</code> and <code>[middle,last)</code>, putting the result of the
merge into the range <code>[first,last)</code>. The resulting range will be in
non-decreasing order; that is, for every iterator <code>i</code> in
<code>[first,last)</code> other than <code>first</code>, the condition <code>*i &lt; *(i -
1)</code> or, respectively, <code>comp(*i, *(i - 1))</code> will be false.
</p>

<p>
7 <i>Requires:</i> <ins>The ranges <code>[first,middle)</code> and
<code>[middle,last)</code> shall be sorted with respect to <code>operator&lt;</code> or
<code>comp</code>.</ins> The type of <code>*first</code> shall satisfy the
<code>Swappable</code> requirements (37), the <code>MoveConstructible</code>
requirements (Table 33), and the the <code>MoveAssignable</code> requirements (Table
35).
</p>
</blockquote>






</body>
</html>
