<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 1308: Concerns about initializer_list overloads of min,
max, and minmax</title>
<meta property="og:title" content="Issue 1308: Concerns about initializer_list overloads of min,
max, and minmax">
<meta property="og:description" content="C++ library issue. Status: NAD">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue1308.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#NAD">NAD</a> status.</em></p>
<h3 id="1308"><a href="lwg-closed.html#1308">1308</a>. Concerns about <code>initializer_list</code> overloads of <code>min</code>,
<code>max</code>, and <code>minmax</code></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#NAD">NAD</a>
 <b>Submitter:</b> Niels Dekker <b>Opened:</b> 2010-02-02 <b>Last modified:</b> 2017-09-07</p>
<p><b>Priority: </b>Not Prioritized
</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#NAD">NAD</a> status.</p>
<p><b>Discussion:</b></p>
<p>
In San Francisco, June 2008, 
<a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2722.pdf">N2722</a>
was adopted, replacing the variadic templates <code>min</code>, <code>max</code>, and
<code>minmax</code> by overloads that have an <code>initializer_list&lt;T&gt;</code>
parameter. The paper showed benchmark results wherein <code>initializer_list</code>
versions of <code>min</code> appeared to outperform the corresponding variadic
template. Unfortunately, in October 2009 a very serious error was detected in
the benchmark. (<a href="http://listarchives.isocpp.org/cgi-bin/wg21/message?wg=lib&amp;msg=25210">c++std-lib-25210</a>).
In fact, an <code>initializer_list&lt;T&gt;</code> version of <code>min</code> often
appears to perform <i>worse</i> than the corresponding variadic template,
especially when <code>T</code> has an expensive copy constructor 
(<a href="http://listarchives.isocpp.org/cgi-bin/wg21/message?wg=lib&amp;msg=25253">c++std-lib-25253</a>,
<a href="http://www.xs4all.nl/~nd/dekkerware/issues/n2772_fix">http://www.xs4all.nl/~nd/dekkerware/issues/n2772_fix</a>).
</p>
<p>
IMO, the biggest problem of the <code>initializer_list</code> overloads is that they
pass and return <code>T</code> objects <i>by value</i>. Which has the following
consequences:
</p>

<ol>
<li>
They require that <code>T</code> is <code>CopyConstructible</code>. IMO that is too much of a
constraint for a generic, general purpose function like
<code>std::min&lt;T&gt;</code>.
</li>
<li>
They potentially throw an exception, even if <code>T</code>'s less-than-operator
throws nothing. (And of course, less-than typically throws nothing.)
</li>
<li>
They are inconsistent with C++03 <code>std::min</code> and <code>std::max</code>.
Consider the subtle difference between <code>const T&amp; c1 = min(a,b);</code> and
<code>const T&amp; c2 = min({a,b});</code> 
(<a href="http://listarchives.isocpp.org/cgi-bin/wg21/message?wg=lib&amp;msg=25265">c++std-lib-25265</a>)
</li>
<li>
They do not conveniently support use cases that need to have a reference to the
minimum or maximum object <i>itself</i>, rather than just a copy.
</li>
<li>
They potentially perform badly: possibly <i>O(n)</i>, when the arguments
themselves have a size of <i>n</i>.
</li>
</ol>

<p>
In the future, this problem might be solvable by using an
<code>initializer_list</code> of <i>const references</i>, instead:
</p>
<blockquote><pre>
const T&amp; min(initializer_list&lt;const T&amp;&gt;);
const T&amp; max(initializer_list&lt;const T&amp;&gt;);
pair&lt;const T&amp;, const T&amp;&gt; minmax(initializer_list&lt;const T&amp;&gt;);
</pre></blockquote>

<p>
It is unlikely that C++0x will support <code>initializer_list&lt;const T&amp;&gt;</code>, 
but technically it seems possible to add such a language
feature after C++0x 
(<a href="http://listarchives.isocpp.org/cgi-bin/wg21/message?wg=core&amp;msg=15428">c++std-core-15428</a>).
</p>
<p>
Variadic templates of <code>min</code>, <code>max</code>, and <code>minmax</code>, as
proposed by 
<a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2551.pdf">N2551</a>
(Sylvain Pion), do have some other advantages over <code>initializer_list</code>
overloads:
</p>
<ol>
<li>
It is likely that those variadic templates can be declared <code>constexpr</code>,
now that 
<a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2009/n3006.html#991">
CWG issue #991</a> is in drafting status.
</li>
<li>
They provide complete compile-time protection against accidentally passing zero
arguments.
</li>
</ol>

<p>
Unfortunately, the variadic templates of <code>min</code>, <code>max</code>, and
<code>minmax</code> may still need further improvement, before having them in the
Standard Library. Especially the optional <code>Compare</code> parameter appears to
be a concern. So for this moment I recommend to keep both versions out of C++0x,
and postpone further discussion until after C++0x.
</p>

<p><i>[
2010 Pittsburgh:  Discussed and the LWG still prefers the initializer list
solution of
<a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2772.pdf">N2772</a>.
]</i></p>




<p><b>Rationale:</b></p><p>
We prefer the solution of
<a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2772.pdf">N2772</a>
which will be reapplied.
</p>

<p id="res-1308"><b>Proposed resolution:</b></p>
<p>
Remove both variadic templates and <code>initializer_list</code> overloads of
<code>min</code>, <code>max</code>, and <code>minmax</code> from the synopsis in
26.1 <a href="https://wg21.link/algorithms.general">[algorithms.general]</a> and from 26.8.9 <a href="https://wg21.link/alg.min.max">[alg.min.max]</a>.
</p>

<blockquote>
<p><i>[
Note: This proposed resolution will resolve LWG <a href="lwg-closed.html#915" title="minmax with initializer_list should return
pair of T, not pair of const T&amp; (Status: NAD Editorial)">915</a><sup><a href="https://cplusplus.github.io/LWG/issue915" title="Latest snapshot">(i)</a></sup> as NAD.
]</i></p>

</blockquote>





</body>
</html>
