<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 3529: priority_queue(first, last) should construct c with (first, last)</title>
<meta property="og:title" content="Issue 3529: priority_queue(first, last) should construct c with (first, last)">
<meta property="og:description" content="C++ library issue. Status: C++23">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue3529.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="3529"><a href="lwg-defects.html#3529">3529</a>. <code>priority_queue(first, last)</code> should construct <code>c</code> with <code>(first, last)</code></h3>
<p><b>Section:</b> 23.6.4 <a href="https://wg21.link/priority.queue">[priority.queue]</a> <b>Status:</b> <a href="lwg-active.html#C++23">C++23</a>
 <b>Submitter:</b> Arthur O'Dwyer <b>Opened:</b> 2021-03-01 <b>Last modified:</b> 2023-11-22</p>
<p><b>Priority: </b>Not Prioritized
</p>
<p><b>View all other</b> <a href="lwg-index.html#priority.queue">issues</a> in [priority.queue].</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>
Tim's new constructors for <code>priority_queue</code> (LWG <a href="lwg-defects.html#3506" title="Missing allocator-extended constructors for priority_queue (Status: C++23)">3506</a><sup><a href="https://cplusplus.github.io/LWG/issue3506" title="Latest snapshot">(i)</a></sup>)
are specified so that when you construct
</p>
<blockquote>
<pre>
auto pq = PQ(first, last, a);
</pre>
</blockquote>
<p>
it calls this new-in-LWG3506 constructor:
</p>
<blockquote>
<pre>
template&lt;class InputIterator, class Alloc&gt;
  priority_queue(InputIterator first, InputIterator last, const Alloc&amp; a);
</pre>
<p>
<i>Effects:</i>
Initializes <code>c</code> with <code>first</code> as the first argument,
<code>last</code> as the second argument,
and <code>a</code> as the third argument,
and value-initializes <code>comp</code>;
calls <code>make_heap(c.begin(), c.end(), comp)</code>.
</p>
</blockquote>
<p>
But the pre-existing constructors are specified so that when you construct
</p>
<blockquote>
<pre>
auto pq = PQ(first, last);
</pre>
</blockquote>
<p>
it calls this pre-existing constructor:
</p>
<blockquote>
<pre>
template&lt;class InputIterator&gt;
  priority_queue(InputIterator first, InputIterator last, const Compare&amp; x = Compare(), Container&amp;&amp; y = Container());
</pre>
<p>
<i>Preconditions:</i>
<code>x</code> defines a strict weak ordering ([alg.sorting]).
</p>
<p>
<i>Effects:</i>
Initializes <code>comp</code> with <code>x</code>
and <code>c</code> with <code>y</code>
(copy constructing or move constructing as appropriate);
calls <code>c.insert(c.end(), first, last)</code>;
and finally calls <code>make_heap(c.begin(), c.end(), comp)</code>.
</p>
</blockquote>

<p>
In other words,
</p>
<blockquote>
<pre>
auto pq = PQ(first, last);
</pre>
</blockquote>
<p>
will default-construct a <code>Container</code>,
then move-construct <code>c</code> from that object,
then <code>c.insert(first, last)</code>,
and finally <code>make_heap</code>.
</p>
<p>
But our new
</p>
<blockquote>
<pre>
auto pq = PQ(first, last, a);
</pre>
</blockquote>
<p>
will simply construct <code>c</code> with <code>(first, last)</code>,
then <code>make_heap</code>.
</p>
<p>
The latter is obviously better.
</p>
<p>
Also, Corentin's <a href="https://wg21.link/p1425r3">P1425R3</a>
specifies the new iterator-pair constructors for
<code>stack</code> and <code>queue</code>
to construct <code>c</code> from <code>(first, last)</code>. Good.
</p>
<p>
LWG should refactor the existing constructor overload set so that
the existing non-allocator-taking constructors simply construct <code>c</code>
from <code>(first, last)</code>.
This will improve consistency with the resolutions of LWG3506 and P1425,
and reduce the surprise factor for users.
</p>

<p><i>[2021-03-12; Reflector poll]</i></p>

<p>
Set status to Tentatively Ready after five votes in favour during reflector poll.
</p>

<p><i>[2021-06-07 Approved at June 2021 virtual plenary. Status changed: Voting &rarr; WP.]</i></p>



<p id="res-3529"><b>Proposed resolution:</b></p>
<p>
This wording is relative to <a href="https://wg21.link/n4878">N4878</a>.
</p>

<ol>
<li>
<p>Edit 23.6.4.1 <a href="https://wg21.link/priqueue.overview">[priqueue.overview]</a> as indicated:</p>

<blockquote>
<pre>
<ins>
template&lt;class InputIterator&gt;
    priority_queue(InputIterator first, InputIterator last, const Compare&amp; x = Compare());</ins>
template&lt;class InputIterator&gt;
    priority_queue(InputIterator first, InputIterator last, const Compare&amp; x, const Container&amp; y);
template&lt;class InputIterator&gt;
    priority_queue(InputIterator first, InputIterator last, const Compare&amp; x<del><del> = Compare()</del></del>, Container&amp;&amp; y<del> = Container()</del>);
</pre>
</blockquote>
</li>

<li>
<p>Edit 23.6.4.2 <a href="https://wg21.link/priqueue.cons">[priqueue.cons]</a> as indicated:</p>

<blockquote>
<pre>
<ins>
template&lt;class InputIterator&gt;
    priority_queue(InputIterator first, InputIterator last, const Compare&amp; x = Compare());
</ins>
</pre>
<p>
<ins>
<i>Preconditions:</i>
<code>x</code> defines a strict weak ordering ([alg.sorting]).
</ins>
</p>
<p>
<ins>
<i>Effects:</i>
Initializes <code>c</code> with <code>first</code> as the first argument and <code>last</code> as the second argument, and initializes <code>comp</code> with <code>x</code>; then calls <code>make_heap(c.begin(), c.end(), comp)</code>.
</ins>
</p>

<pre>
template&lt;class InputIterator&gt;
    priority_queue(InputIterator first, InputIterator last, const Compare&amp; x, const Container&amp; y);
template&lt;class InputIterator&gt;
    priority_queue(InputIterator first, InputIterator last, const Compare&amp; x<del> = Compare()</del>, Container&amp;&amp; y<del> = Container()</del>);
</pre>
<p>
<i>Preconditions:</i>
<code>x</code> defines a strict weak ordering ([alg.sorting]).
</p>
<p>
<i>Effects:</i>
Initializes <code>comp</code> with <code>x</code> and <code>c</code> with <code>y</code> (copy constructing or
move constructing as appropriate); calls <code>c.insert(c.end(), first, last)</code>;
and finally calls <code>make_heap(c.begin(), c.end(), comp)</code>.
</p>
</blockquote>
</li>
</ol>





</body>
</html>
