<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 3521: Overly strict requirements on qsort and bsearch</title>
<meta property="og:title" content="Issue 3521: Overly strict requirements on qsort and bsearch">
<meta property="og:description" content="C++ library issue. Status: C++23">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue3521.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="3521"><a href="lwg-defects.html#3521">3521</a>. Overly strict requirements on <code>qsort</code> and <code>bsearch</code></h3>
<p><b>Section:</b> 26.13 <a href="https://wg21.link/alg.c.library">[alg.c.library]</a> <b>Status:</b> <a href="lwg-active.html#C++23">C++23</a>
 <b>Submitter:</b> Richard Smith <b>Opened:</b> 2021-02-02 <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#alg.c.library">issues</a> in [alg.c.library].</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>
Per 26.13 <a href="https://wg21.link/alg.c.library">[alg.c.library]</a>/2, for <code>qsort</code> and <code>bsearch</code>, we have:
</p>
<blockquote>
<p>
<i>Preconditions:</i> The objects in the array pointed to by <code>base</code> are of trivial type.
</p>
</blockquote>
<p>
This seems like an unnecessarily strict requirement. <code>qsort</code> only needs the objects to be of a 
trivially-copyable type (because it will use <code>memcpy</code> or equivalent to relocate them), and 
<code>bsearch</code> doesn't need any particular properties of the array element type. Presumably it 
would be in improvement to specify the more-precise requirements instead.
<p/>
We should also reconsider the other uses of the notion of a trivial type. It's really not a useful or 
meaningful type property by itself, because it doesn't actually require that any operations on the type 
are valid (due to the possibility of them being ambiguous or only some of them being available) and the 
places that consider it very likely actually mean <code>is_trivially_copyable</code> plus 
<code>is_trivially_default_constructible</code> instead, or perhaps <code>is_trivially_copy_constructible</code> 
and <code>is_trivially_move_constructible</code> and so on.
<p/>
Other than <code>qsort</code> and <code>bsearch</code>, the only uses of this type property in the standard are 
to constrain <code>max_align_t</code>, <code>aligned_storage</code>, <code>aligned_union</code>, and the element type 
of <code>basic_string</code> (and in the definition of the deprecated <code>is_pod</code> trait), all of which 
(other than <code>is_pod</code>) I think really mean "is trivially default constructible", not "has at least 
one eligible default constructor and all eligible default constructors are trivial". And in fact I think 
the alignment types are underspecified &mdash; we don't want to require merely that they be 
trivially-copyable, since that doesn't require any particular operation on them to actually be valid &mdash; 
we also want to require that they actually model <code>semiregular</code>.
</p>

<p><i>[2021-02-23; Casey Carter provides concrete wording]</i></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-3521"><b>Proposed resolution:</b></p>
<p>
This wording is relative to <a href="https://wg21.link/n4878">N4878</a>. 
</p>

<ol>
<li><p>Modify 26.13 <a href="https://wg21.link/alg.c.library">[alg.c.library]</a> as indicated:</p>

<blockquote>
<pre>
void* bsearch(const void* key, const void* base, size_t nmemb, size_t size,
             <i>c-compare-pred</i>* compar);
void* bsearch(const void* key, const void* base, size_t nmemb, size_t size,
              <i>compare-pred</i>* compar);
void qsort(void* base, size_t nmemb, size_t size, <i>c-compare-pred</i>* compar);
void qsort(void* base, size_t nmemb, size_t size, <i>compare-pred</i>* compar);
</pre>
<blockquote>
<p>
-2- <i>Preconditions:</i> <ins>For <code>qsort</code>, t</ins><del>T</del>he objects in the array pointed to 
by <i>base</i> are of <del>trivial</del><ins>trivially copyable</ins> type.
</p>
</blockquote>
</blockquote>
</li>
</ol>





</body>
</html>
