<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 2232: [CD] The char_traits specializations should declare their length(), compare(), and 
find() members constexpr</title>
<meta property="og:title" content="Issue 2232: [CD] The char_traits specializations should declare their length(), compare(), and 
find() members constexpr">
<meta property="og:description" content="C++ library issue. Status: Resolved">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue2232.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#Resolved">Resolved</a> status.</em></p>
<h3 id="2232"><a href="lwg-defects.html#2232">2232</a>. [CD] The <code>char_traits</code> specializations should declare their <code>length()</code>, <code>compare()</code>, and 
<code>find()</code> members constexpr</h3>
<p><b>Section:</b> 27.2.4 <a href="https://wg21.link/char.traits.specializations">[char.traits.specializations]</a> <b>Status:</b> <a href="lwg-active.html#Resolved">Resolved</a>
 <b>Submitter:</b> Jeffrey Yasskin <b>Opened:</b> 2012-12-24 <b>Last modified:</b> 2020-09-06</p>
<p><b>Priority: </b>Not Prioritized
</p>
<p><b>View all other</b> <a href="lwg-index.html#char.traits.specializations">issues</a> in [char.traits.specializations].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#Resolved">Resolved</a> status.</p>
<p><b>Discussion:</b></p>

<p><b>Addresses ES 14, US 19</b></p>

<p>
These functions have easy recursive constexpr implementations that, unfortunately, aren't efficient at runtime. 
EWG is still figuring out how to solve this problem in general (e.g., 
<a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3444.html">N3444</a> isn't sufficient to avoid 
stack overflows in debug builds or to get the optimal assembly-based implementations at runtime), so users can't 
portably solve this problem for themselves, but implementations can use compiler-specific techniques to choose 
the right implementation inside their standard libraries.
</p>

<p>
The LWG is still undecided about whether individual implementations can add constexpr to these functions, so we 
need to add <code>constexpr</code> to the standard here for implementations to be able to improve this.
</p>

<p><i>[2013-03-15 Issues Teleconference]</i></p>

<p>
Moved to Open.
</p>
<p>
There are a number of people who have a strong interest in this issue not available for the telecon.
</p>
<p>
It also plays at the heart of a discussion about library freedoms for <code>constexpr</code> and specifying
a library that may depend on unspecified compiler intrinsics to be implementable.
</p>

<p><i>[2013-09 Chicago]</i></p>

<p>
Moved to NAD Future.
</p>
<p>
While it is clear that this feature can be implemented using only C++14 <code>constexpr</code> features,
there is real concern that we cannot call the efficient, highly optimized, C implementations of these
functions under a C++14 <code>constexpr</code> implementation, nor implement similar ourselves as this
typically involves use of inline <code>asm</code> instructions.
</p>
<p>
Clang and libc++ have some experience of using intrinsics to try to address the performance issue, but
the current intrinsics are not general enough to support <code>char_traits</code>.  The intrinsics support
only operations on character string literals, and the string literal is no longer visible <i>as</i> a
literal after passing as a <code>const char *</code> to the <code>char_traits</code> functions.
</p>
<p>
Additional concern was raised that these operations are unlikely to be useful anyway, as the only client
is <code>basic_string</code> which relies on dynamic memory allocation, and so cannot effectively be made a
literal type.  Jeffrey then pointed out the pending <code>string_view</code> library that will also use
<code>char_traits</code> and would most certainly benefit from being a literal type.
</p>

<p>
Given the choice of giving up performance on a critical library component, or requiring a compiler
intrinsic with only unsuccessful implementation experience, the consensus is to not reject this, unless
compelling implementation experience is demonstrated.  NAD Future seems the appropriate resolution.
</p>

<p><i>[2017-06-02 Issues Telecon]</i></p>

<p>Resolved by <a href="https://wg21.link/P0426R1">P0426R1</a>, adopted in Issaquah.</p>


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

<ol>
<li><p>In 27.2.4.2 <a href="https://wg21.link/char.traits.specializations.char">[char.traits.specializations.char]</a>,  [char.traits.specializations.char16_t], 
 [char.traits.specializations.char32_t], and 27.2.4.6 <a href="https://wg21.link/char.traits.specializations.wchar.t">[char.traits.specializations.wchar.t]</a>:</p>
<blockquote><pre>
static <ins>constexpr</ins> int compare(const char_type* s1, const char_type* s2, size_t n);
static <ins>constexpr</ins> size_t length(const char_type* s);
static <ins>constexpr</ins> const char_type* find(const char_type* s, size_t n, const char_type&amp; a);
</pre></blockquote>
</li>
</ol>






</body>
</html>
