<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 3032: ValueSwappable requirement missing for push_heap and make_heap</title>
<meta property="og:title" content="Issue 3032: ValueSwappable requirement missing for push_heap and make_heap">
<meta property="og:description" content="C++ library issue. Status: C++23">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue3032.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++23">C++23</a> status.</em></p>
<h3 id="3032"><a href="lwg-defects.html#3032">3032</a>. <code>ValueSwappable</code> requirement missing for <code>push_heap</code> and <code>make_heap</code></h3>
<p><b>Section:</b> 26.8.8 <a href="https://wg21.link/alg.heap.operations">[alg.heap.operations]</a> <b>Status:</b> <a href="lwg-active.html#C++23">C++23</a>
 <b>Submitter:</b> Robert Douglas <b>Opened:</b> 2017-11-08 <b>Last modified:</b> 2023-11-22</p>
<p><b>Priority: </b>3
</p>
<p><b>View all other</b> <a href="lwg-index.html#alg.heap.operations">issues</a> in [alg.heap.operations].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#C++23">C++23</a> status.</p>
<p><b>Discussion:</b></p>
<p>
In discussion of D0202R3 in Albuquerque, it was observed that <code>pop_heap</code> and <code>sort_heap</code> had 
<code>constexpr</code> removed for their requirement of <code>ValueSwappable</code>. It was then observed that 
<code>push_heap</code> and <code>make_heap</code> were not similarly marked as having the <code>ValueSwappable</code> requirement. 
The room believed this was likely a specification error, and asked to open an issue to track it.
</p>

<p><i>[2017-11 Albuquerque Wednesday night issues processing]</i></p>

<p>Priority set to 3; Marshall to investigate</p>

<p><strong>Previous resolution [SUPERSEDED]:</strong></p>
<blockquote class="note">
<p>This wording is relative to <a href="https://wg21.link/n4700">N4700</a>.</p>

<ol>
<li><p>Change 26.8.8.2 <a href="https://wg21.link/push.heap">[push.heap]</a> as indicated:</p>

<blockquote>
<pre>
template&lt;class RandomAccessIterator&gt;
  void push_heap(RandomAccessIterator first, RandomAccessIterator last);
template&lt;class RandomAccessIterator, class Compare&gt;
  void push_heap(RandomAccessIterator first, RandomAccessIterator last,
                 Compare comp);
</pre>
<blockquote>
<p>
-1- <i>Requires:</i> The range <code>[first, last - 1)</code> shall be a valid heap. <ins><code>RandomAccessIterator</code> shall satisfy 
the requirements of <code>ValueSwappable</code> (16.4.4.3 <a href="https://wg21.link/swappable.requirements">[swappable.requirements]</a>).</ins> The type of <code>*first</code> 
shall satisfy the <code>MoveConstructible</code> requirements (Table 23) and the <code>MoveAssignable</code> requirements (Table 25).
</p>
</blockquote>
</blockquote>
</li>

<li><p>Change 26.8.8.4 <a href="https://wg21.link/make.heap">[make.heap]</a> as indicated:</p>

<blockquote>
<pre>
template&lt;class RandomAccessIterator&gt;
  void make_heap(RandomAccessIterator first, RandomAccessIterator last);
template&lt;class RandomAccessIterator, class Compare&gt;
  void make_heap(RandomAccessIterator first, RandomAccessIterator last,
                 Compare comp);
</pre>
<blockquote>
<p>
-1- <i>Requires:</i> <ins><code>RandomAccessIterator</code> shall satisfy the requirements of <code>ValueSwappable</code> 
(16.4.4.3 <a href="https://wg21.link/swappable.requirements">[swappable.requirements]</a>).</ins> The type of <code>*first</code> shall satisfy the <code>MoveConstructible</code> 
requirements (Table 23) and the <code>MoveAssignable</code> requirements (Table 25).
</p>
</blockquote>
</blockquote>
</li>
</ol>
</blockquote>

<p><i>[2022-11-06; Daniel comments and syncs wording with recent working draft]</i></p>

<p>
For reference, the finally accepted paper was <a href="https://wg21.link/P0202R3" title=" Add Constexpr Modifiers to Functions in <algorithm> and <utility> Headers">P0202R3</a> and the constexpr-ification of
swap-related algorithms had been realized later by <a href="https://wg21.link/P0879R0" title=" Constexpr for swap and swap related functions">P0879R0</a> after resolution of
<a href="https://wg21.link/cwg1581">CWG 1581</a> and more importantly <a href="https://wg21.link/cwg1330">CWG 1330</a>.
</p>

<p><i>[Kona 2022-11-09; Move to Ready]</i></p>

<p><i>[2023-02-13 Approved at February 2023 meeting in Issaquah. Status changed: Voting &rarr; WP.]</i></p>



<p id="res-3032"><b>Proposed resolution:</b></p>
<p>
This wording is relative to <a href="https://wg21.link/N4917" title=" Working Draft, Standard for Programming Language C++">N4917</a>.
</p>

<ol>
<li><p>Change 26.8.8.2 <a href="https://wg21.link/push.heap">[push.heap]</a> as indicated:</p>

<blockquote>
<pre>
template&lt;class RandomAccessIterator&gt;
  constexpr void push_heap(RandomAccessIterator first, RandomAccessIterator last);

template&lt;class RandomAccessIterator, class Compare&gt;
  constexpr void push_heap(RandomAccessIterator first, RandomAccessIterator last,
                           Compare comp);
                 
template&lt;random_access_iterator I, sentinel_for&lt;I&gt; S, class Comp = ranges::less,
         class Proj = identity&gt;
  requires sortable&lt;I, Comp, Proj&gt;
  constexpr I
    ranges::push_heap(I first, S last, Comp comp = {}, Proj proj = {});
template&lt;random_access_range R, class Comp = ranges::less, class Proj = identity&gt;
  requires sortable&lt;iterator_t&lt;R&gt;, Comp, Proj&gt;
  constexpr borrowed_iterator_t&lt;R&gt;
    ranges::push_heap(R&amp;&amp; r, Comp comp = {}, Proj proj = {});
</pre>
<blockquote>
<p>
-1- Let <code>comp</code> be <code>less{}</code> and <code>proj</code> be <code>identity{}</code> for the overloads with no parameters by those names.
<p/>
-2- <i>Preconditions</i>: The range <code>[first, last - 1)</code> is a valid heap with respect to 
<code>comp</code> and <code>proj</code>. For the overloads in namespace <code>std</code>, 
<ins><code>RandomAccessIterator</code> meets the <i>Cpp17ValueSwappable</i> requirements 
(16.4.4.3 <a href="https://wg21.link/swappable.requirements">[swappable.requirements]</a>) and</ins> the type of <code>*first</code> meets the 
<i>Cpp17MoveConstructible</i> requirements (Table 32) and the <i>Cpp17MoveAssignable</i> 
requirements (Table 34).
<p/>
-3- <i>Effects</i>: Places the value in the location <code>last - 1</code> into the resulting heap <code>[first, last)</code>.
<p/>
-4- <i>Returns</i>: <code>last</code> for the overloads in namespace <code>ranges</code>.
<p/>
-5- <i>Complexity</i>: At most log(<code>last - first</code>) comparisons and twice as many projections.
</p>
</blockquote>
</blockquote>
</li>

<li><p>Change 26.8.8.4 <a href="https://wg21.link/make.heap">[make.heap]</a> as indicated:</p>

<blockquote>
<pre>
template&lt;class RandomAccessIterator&gt;
  constexpr void make_heap(RandomAccessIterator first, RandomAccessIterator last);

template&lt;class RandomAccessIterator, class Compare&gt;
  constexpr void make_heap(RandomAccessIterator first, RandomAccessIterator last,
                           Compare comp);
                           
template&lt;random_access_iterator I, sentinel_for&lt;I&gt; S, class Comp = ranges::less,
         class Proj = identity&gt;
  requires sortable&lt;I, Comp, Proj&gt;
  constexpr I
    ranges::make_heap(I first, S last, Comp comp = {}, Proj proj = {});
template&lt;random_access_range R, class Comp = ranges::less, class Proj = identity&gt;
  requires sortable&lt;iterator_t&lt;R&gt;, Comp, Proj&gt;
  constexpr borrowed_iterator_t&lt;R&gt;
    ranges::make_heap(R&amp;&amp; r, Comp comp = {}, Proj proj = {});
</pre>
<blockquote>
<p>
-1- Let <code>comp</code> be <code>less{}</code> and <code>proj</code> be <code>identity{}</code> for the overloads with no parameters by those names.
<p/>
-2- <i>Preconditions</i>: For the overloads in namespace <code>std</code>, <ins><code>RandomAccessIterator</code> 
meets the <i>Cpp17ValueSwappable</i> requirements (16.4.4.3 <a href="https://wg21.link/swappable.requirements">[swappable.requirements]</a>) and</ins> the type 
of <code>*first</code> meets the <i>Cpp17MoveConstructible</i> (Table 32) and <i>Cpp17MoveAssignable</i> (Table 34) requirements.
<p/>
-3- <i>Effects</i>: Constructs a heap with respect to <code>comp</code> and <code>proj</code> out of the range <code>[first, last)</code>.
<p/>
-4- <i>Returns</i>: <code>last</code> for the overloads in namespace <code>ranges</code>.
<p/>
-5- <i>Complexity</i>: At most 3(<code>last - first</code>) comparisons and twice as many projections.
</p>
</blockquote>
</blockquote>
</li>
</ol>





</body>
</html>
