<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 2239: min/max/minmax requirements</title>
<meta property="og:title" content="Issue 2239: min/max/minmax requirements">
<meta property="og:description" content="C++ library issue. Status: C++17">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue2239.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++17">C++17</a> status.</em></p>
<h3 id="2239"><a href="lwg-defects.html#2239">2239</a>. <code>min/max/minmax</code> requirements</h3>
<p><b>Section:</b> 26.8.9 <a href="https://wg21.link/alg.min.max">[alg.min.max]</a> <b>Status:</b> <a href="lwg-active.html#C++17">C++17</a>
 <b>Submitter:</b> Juan Soulie <b>Opened:</b> 2013-01-26 <b>Last modified:</b> 2017-07-30</p>
<p><b>Priority: </b>3
</p>
<p><b>View other</b> <a href="lwg-index-open.html#alg.min.max">active issues</a> in [alg.min.max].</p>
<p><b>View all other</b> <a href="lwg-index.html#alg.min.max">issues</a> in [alg.min.max].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#C++17">C++17</a> status.</p>
<p><b>Discussion:</b></p>
<p>
26.8.9 <a href="https://wg21.link/alg.min.max">[alg.min.max]</a> requires type <code>T</code> in <code>min</code>, <code>max</code>, and <code>minmax</code> to be 
<code>LessThanComparable</code>, but I don't believe this should be required for the versions that take a <code>Compare</code> 
argument.
<p/>
Paragraphs 1 to 4 of 26.8 <a href="https://wg21.link/alg.sorting">[alg.sorting]</a> should apply anyway, although I'm not sure about <code>Compare</code> 
being required to induce a strict weak ordering here.
<p/>
Further, <code>min</code> and <code>max</code> also lack formal complexity guarantees.
</p>

<p><i>[2014-06-07 Daniel comments and provides wording]</i></p>


<p>
Certainly, the functions with <code>Compare</code> should not impose <code>LessThanComparable</code> requirements.
<p/>
In regard to the question whether a strict weak ordering should be required as implied by the <code>Compare</code>
requirements, I would like to point out that this is requirement is in fact needed, because the specification of 
the normative <i>Remarks</i> elements (e.g. "Returns the first argument when the arguments are equivalent.") do depend 
on the existence of a <em>equivalence relation</em> that can be relied on and this is also consistent with the same
strict weak ordering requirement that is indirectly imposed by the <code>LessThanComparable</code> requirement set for 
functions referring to <code>operator&lt;</code> (Let me note that the very same <code>StrictWeakOrder</code> language 
concept had intentionally been required for similar reasons during "concept-time" in
<a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2009/n2914.pdf">N2914</a>).
</p>

<p><i>[2015-02 Cologne]</i></p>

<p>
JY: We have library-wide requirements that <code>Comp</code> induce a strict weak ordering.
<p/>
JY/MC: The un-marked-up "Complexity" (p16) is wrong. DK: I'll fix that.
<p/>
DK will update the wording for Lenexa. 
</p>

<p><i>[2015-03-30 Daniel comments]</i></p>

<p>
The Complexity element of p16 is correct, but some others involving <code>initializer_list</code> arguments are wrong. 
</p>

<p><i>[2015-04-02 Library reflector vote]</i></p>

<p>
The issue has been identified as Tentatively Ready based on six votes in favour.
</p>


<p id="res-2239"><b>Proposed resolution:</b></p>
<p>This wording is relative to N4296.</p>

<ol>
<li><p>Change 26.8.9 <a href="https://wg21.link/alg.min.max">[alg.min.max]</a> as indicated:
</p>

<blockquote>
<pre>
template&lt;class T&gt; constexpr const T&amp; min(const T&amp; a, const T&amp; b);
template&lt;class T, class Compare&gt;
  constexpr const T&amp; min(const T&amp; a, const T&amp; b, Compare comp);
</pre>
<blockquote>
<p>
-1- <i>Requires</i>: <ins>For the first form, type <code>T</code> shall be</ins><del>Type <code>T</code> is</del> <code>LessThanComparable</code> 
(Table 18).
<p/>
-2- <i>Returns</i>: The smaller value.
<p/>
-3- <i>Remarks</i>: Returns the first argument when the arguments are equivalent.
<p/>
<ins>-?- <i>Complexity</i>: Exactly one comparison.</ins>
</p>
</blockquote>

<pre>
template&lt;class T&gt;
  constexpr T min(initializer_list&lt;T&gt; t);
template&lt;class T, class Compare&gt;
  constexpr T min(initializer_list&lt;T&gt; t, Compare comp);
</pre>
<blockquote>
<p>
-4- <i>Requires</i>: <code>T</code> <del>is <code>LessThanComparable</code> and</del><ins>shall be</ins> <code>CopyConstructible</code> and 
<code>t.size() &gt; 0</code>. <ins>For the first form, type <code>T</code> shall be <code>LessThanComparable</code>.</ins>
<p/>
-5- <i>Returns</i>: [&hellip;]
<p/>
-6- <i>Remarks</i>: [&hellip;]
<p/>
<ins>-?- <i>Complexity</i>: Exactly <code>t.size() - 1</code> comparisons.</ins>
</p>
</blockquote>

<pre>
template&lt;class T&gt; constexpr const T&amp; max(const T&amp; a, const T&amp; b);
template&lt;class T, class Compare&gt;
  constexpr const T&amp; max(const T&amp; a, const T&amp; b, Compare comp);
</pre>
<blockquote>
<p>
-7- <i>Requires</i>: <ins>For the first form, type <code>T</code> shall be</ins><del>Type <code>T</code> is</del> <code>LessThanComparable</code> 
(Table 18).
<p/>
-8- <i>Returns</i>: [&hellip;]
<p/>
-9- <i>Remarks</i>: [&hellip;]
<p/>
<ins>-?- <i>Complexity</i>: Exactly one comparison.</ins>
</p>
</blockquote>

<pre>
template&lt;class T&gt;
  constexpr T max(initializer_list&lt;T&gt; t);
template&lt;class T, class Compare&gt;
  constexpr T max(initializer_list&lt;T&gt; t, Compare comp);
</pre>
<blockquote>
<p>
-10- <i>Requires</i>: <code>T</code> <del>is <code>LessThanComparable</code> and</del><ins>shall be</ins> <code>CopyConstructible</code> and 
<code>t.size() &gt; 0</code>. <ins>For the first form, type <code>T</code> shall be <code>LessThanComparable</code>.</ins>
<p/>
-11- <i>Returns</i>: [&hellip;]
<p/>
-12- <i>Remarks</i>: [&hellip;]
<p/>
<ins>-?- <i>Complexity</i>: Exactly <code>t.size() - 1</code> comparisons.</ins>
</p>
</blockquote>

<pre>
template&lt;class T&gt; constexpr pair&lt;const T&amp;, const T&amp;&gt; minmax(const T&amp; a, const T&amp; b);
template&lt;class T, class Compare&gt;
  constexpr pair&lt;const T&amp;, const T&amp;&gt; minmax(const T&amp; a, const T&amp; b, Compare comp);
</pre>
<blockquote>
<p>
-13- <i>Requires</i>: <ins>For the first form, t</ins><del>T</del>ype <code>T</code> shall be <code>LessThanComparable</code> 
(Table 18).
<p/>
-14- <i>Returns</i>: [&hellip;]
<p/>
-15- <i>Remarks</i>: [&hellip;]
<p/>
-16- <i>Complexity</i>: Exactly one comparison.
</p>
</blockquote>

<pre>
template&lt;class T&gt;
  constexpr pair&lt;T, T&gt; minmax(initializer_list&lt;T&gt; t);
template&lt;class T, class Compare&gt;
  constexpr pair&lt;T, T&gt; minmax(initializer_list&lt;T&gt; t, Compare comp);
</pre>
<blockquote>
<p>
-17- <i>Requires</i>: <code>T</code> <del>is <code>LessThanComparable</code> and</del><ins>shall be</ins> <code>CopyConstructible</code> and 
<code>t.size() &gt; 0</code>. <ins>For the first form, type <code>T</code> shall be <code>LessThanComparable</code>.</ins>
<p/>
-18- <i>Returns</i>: [&hellip;]
<p/>
-19- <i>Remarks</i>: [&hellip;]
<p/>
-20- <i>Complexity</i>: At most <code>(3/2) * t.size()</code> applications of the corresponding predicate.
</p>
</blockquote>

</blockquote>
</li>

</ol>





</body>
</html>
