<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 3950: std::basic_string_view comparison operators are overspecified</title>
<meta property="og:title" content="Issue 3950: std::basic_string_view comparison operators are overspecified">
<meta property="og:description" content="C++ library issue. Status: WP">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue3950.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#WP">WP</a> status.</em></p>
<h3 id="3950"><a href="lwg-defects.html#3950">3950</a>. <code>std::basic_string_view</code> comparison operators are overspecified</h3>
<p><b>Section:</b> 27.3.2 <a href="https://wg21.link/string.view.synop">[string.view.synop]</a> <b>Status:</b> <a href="lwg-active.html#WP">WP</a>
 <b>Submitter:</b> Giuseppe D'Angelo <b>Opened:</b> 2023-06-21 <b>Last modified:</b> 2024-04-02</p>
<p><b>Priority: </b>Not Prioritized
</p>
<p><b>View all issues with</b> <a href="lwg-status.html#WP">WP</a> status.</p>
<p><b>Discussion:</b></p>
<p>
The <code>&lt;string_view&gt;</code> synopsis in 27.3.2 <a href="https://wg21.link/string.view.synop">[string.view.synop]</a> has these signatures
for <code>operator==</code> and <code>operator&lt;=&gt;</code>:
</p>
<blockquote><pre>
// <i>27.3.4 <a href="https://wg21.link/string.view.comparison">[string.view.comparison]</a>, non-member comparison functions</i>
template&lt;class charT, class traits&gt;
  constexpr bool operator==(basic_string_view&lt;charT, traits&gt; x,
                            basic_string_view&lt;charT, traits&gt; y) noexcept;
template&lt;class charT, class traits&gt;
  constexpr see below operator&lt;=&gt;(basic_string_view&lt;charT, traits&gt; x,
                                  basic_string_view&lt;charT, traits&gt; y) noexcept;

// <i>see 27.3.4 <a href="https://wg21.link/string.view.comparison">[string.view.comparison]</a>, sufficient additional overloads of comparison functions</i>
</pre></blockquote>
<p>
In 27.3.4 <a href="https://wg21.link/string.view.comparison">[string.view.comparison]</a>, paragraph 1 states that "Implementations
shall provide sufficient additional overloads" so that all comparisons
between a <code>basic_string_view&lt;C, T&gt;</code> object and an object of a type
convertible to <code>basic_string_view&lt;C, T&gt;</code> work (with the reasonable
semantics).
<p/>
The associated Example 1 proposes this implementation strategy for
<code>operator==</code>:
</p>
<blockquote><pre>
template&lt;class charT, class traits&gt;
  constexpr bool operator==(basic_string_view&lt;charT, traits&gt; lhs,
                            basic_string_view&lt;charT, traits&gt; rhs) noexcept {
    return lhs.compare(rhs) == 0;
  }
template&lt;class charT, class traits&gt;
  constexpr bool operator==(basic_string_view&lt;charT, traits&gt; lhs,
                            type_identity_t&lt;basic_string_view&lt;charT, traits&gt;&gt; rhs) noexcept {
    return lhs.compare(rhs) == 0;
  }
</pre></blockquote>
<p>
With the current semantics of rewritten candidates for the comparison
operators, it is however superfluous to actually specify both overloads
(the same applies for <code>operator&lt;=&gt;</code>).
<p/>
The second overload (using <code>type_identity_t</code>) is indeed necessary to
implement the "sufficient additional overloads" part of 27.3.4 <a href="https://wg21.link/string.view.comparison">[string.view.comparison]</a>, 
but it is also sufficient, as all the following cases
</p>
<ul>
<li><p><code>sv == sv</code></p></li>
<li><p><code>sv == <i>convertible_to_sv</i></code></p></li>
<li><p><code><i>convertible_to_sv</i> == sv</code></p></li>
</ul>
<p>
can in fact use it (directly, or after being rewritten e.g. with the
arguments swapped).
<p/>
The reason why we still do have both operators seems to be historical;
there is an explanation offered <a href="https://stackoverflow.com/a/70851101">here</a> by Barry Revzin.
<p/>
Basically, there were three overloads before a bunch of papers regarding
<code>operator&lt;=&gt;</code> and <code>operator==</code> were merged:
</p>
<ol>
<li><p><code>operator==(<i>bsv</i>, <i>bsv</i>)</code> to deal with <code>sv == sv</code>;</p></li>
<li><p><code>operator==(<i>bsv</i>, type_identity_t&lt;<i>bsv</i>&gt;)</code> and</p></li>
<li><p><code>operator==(type_identity_t&lt;<i>bsv</i>&gt;, <i>bsv</i>)</code> to deal with 
<code>sv == <i>convertible_to_sv</i></code> and vice versa.</p></li>
</ol>
<p>
Overload (1) was necessary because with only (2) and (3) a call like 
<code>sv == sv</code> would otherwise be ambiguous. With the adoption of the rewriting
rules, overload (3) has been dropped, without realizing that overload
(1) would then become redundant.
<p/>
The specification of these overloads can be greatly simplified by
adjusting the signatures to explicitly use <code>type_identity_t</code>.
</p>

<p><i>[Kona 2023-11-10; move to Ready]</i></p>

<p>
Editorial issue <a href="https://github.com/cplusplus/draft/pull/6324">6324</a>
provides the changes as a pull request to the draft.
</p>
<p><i>[Tokyo 2024-03-23; Status changed: Voting &rarr; WP.]</i></p>



<p id="res-3950"><b>Proposed resolution:</b></p>
<p>
This wording is relative to <a href="https://wg21.link/N4950" title=" Working Draft, Standard for Programming Language C++">N4950</a>.
</p>

<ol>
<li><p>Modify 27.3.2 <a href="https://wg21.link/string.view.synop">[string.view.synop]</a>, header <code>&lt;string_view&gt;</code> synopsis, as indicated:</p>

<blockquote>
<pre>
[&hellip;]
// <i>27.3.4 <a href="https://wg21.link/string.view.comparison">[string.view.comparison]</a>, non-member comparison functions</i>
template&lt;class charT, class traits&gt;
  constexpr bool operator==(basic_string_view&lt;charT, traits&gt; x,
                            <ins>type_identity_t&lt;</ins>basic_string_view&lt;charT, traits&gt;<ins>&gt;</ins> y) noexcept;

template&lt;class charT, class traits&gt;
  constexpr <i>see below</i> operator&lt;=&gt;(basic_string_view&lt;charT, traits&gt; x,
                                  <ins>type_identity_t&lt;</ins>basic_string_view&lt;charT, traits&gt;<ins>&gt;</ins> y) noexcept;

<del>// <i>see 27.3.4 <a href="https://wg21.link/string.view.comparison">[string.view.comparison]</a>, sufficient additional overloads of comparison functions</i></del>
[&hellip;]
</pre>
</blockquote>
</li>

<li><p>Modify 27.3.4 <a href="https://wg21.link/string.view.comparison">[string.view.comparison]</a> as indicated:</p>

<blockquote>
<p>
<del>-1- Let <code>S</code> be <code>basic_string_view&lt;charT, traits&gt;</code>, and <code>sv</code> be an instance of <code>S</code>. 
Implementations shall provide sufficient additional overloads marked <code>constexpr</code> and <code>noexcept</code> so that an 
object <code>t</code> with an implicit conversion to <code>S</code> can be compared according to Table 81 
[tab:string.view.comparison.overloads].</del>
</p>

<table border="1">
<caption><del>Table 81: Additional <code>basic_string_view</code> comparison overloads [tab:string.view.comparison.overloads]</del></caption>
<tr>
<th align="center"><del>Expression</del></th>
<th align="center"><del>Equivalent to</del></th>
</tr>

<tr>
<td><del>
<code>t == sv</code>
</del></td>
<td><del>
<code>S(t) == sv</code>
</del></td>
</tr>

<tr>
<td><del>
<code>sv == t</code>
</del></td>
<td><del>
<code>sv == S(t)</code>
</del></td>
</tr>

<tr>
<td><del>
<code>t != sv</code>
</del></td>
<td><del>
<code>S(t) != sv</code>
</del></td>
</tr>

<tr>
<td><del>
<code>sv != t</code>
</del></td>
<td><del>
<code>sv != S(t)</code>
</del></td>
</tr>

<tr>
<td><del>
<code>t &lt; sv</code>
</del></td>
<td><del>
<code>S(t) &lt; sv</code>
</del></td>
</tr>

<tr>
<td><del>
<code>sv &lt; t</code>
</del></td>
<td><del>
<code>sv &lt; S(t)</code>
</del></td>
</tr>

<tr>
<td><del>
<code>t &gt; sv</code>
</del></td>
<td><del>
<code>S(t) &gt; sv</code>
</del></td>
</tr>

<tr>
<td><del>
<code>sv &gt; t</code>
</del></td>
<td><del>
<code>sv &gt; S(t)</code>
</del></td>
</tr>

<tr>
<td><del>
<code>t &lt;= sv</code>
</del></td>
<td><del>
<code>S(t) &lt;= sv</code>
</del></td>
</tr>

<tr>
<td><del>
<code>sv &lt;= t</code>
</del></td>
<td><del>
<code>sv &lt;= S(t)</code>
</del></td>
</tr>

<tr>
<td><del>
<code>t &gt;= sv</code>
</del></td>
<td><del>
<code>S(t) &gt;= sv</code>
</del></td>
</tr>

<tr>
<td><del>
<code>sv &gt;= t</code>
</del></td>
<td><del>
<code>sv &gt;= S(t)</code>
</del></td>
</tr>

<tr>
<td><del>
<code>t &lt;=&gt; sv</code>
</del></td>
<td><del>
<code>S(t) &lt;=&gt; sv</code>
</del></td>
</tr>

<tr>
<td><del>
<code>sv &lt;=&gt; t</code>
</del></td>
<td><del>
<code>sv &lt;=&gt; S(t)</code>
</del></td>
</tr>

</table>
<p>
<del>[<i>Example 1</i>: A sample conforming implementation for <code>operator==</code> would be:</del>
</p>
<blockquote><pre>
<del>template&lt;class charT, class traits&gt;
  constexpr bool operator==(basic_string_view&lt;charT, traits&gt; lhs,
                            basic_string_view&lt;charT, traits&gt; rhs) noexcept {
    return lhs.compare(rhs) == 0;
  }
template&lt;class charT, class traits&gt;
  constexpr bool operator==(basic_string_view&lt;charT, traits&gt; lhs,
                            type_identity_t&lt;basic_string_view&lt;charT, traits&gt;&gt; rhs) noexcept {
    return lhs.compare(rhs) == 0;
  }</del>
</pre></blockquote>
<p>
<del>&mdash; <i>end example</i>]</del>
</p>
<pre>
template&lt;class charT, class traits&gt;
  constexpr bool operator==(basic_string_view&lt;charT, traits&gt; lhs,
                            <ins>type_identity_t&lt;</ins>basic_string_view&lt;charT, traits&gt;<ins>&gt;</ins> rhs) noexcept;

</pre>
<blockquote>
<p>
-2- <i>Returns:</i> <code>lhs.compare(rhs) == 0</code>.
</p>
</blockquote>
<pre>
template&lt;class charT, class traits&gt;
  constexpr <i>see below</i> operator&lt;=&gt;(basic_string_view&lt;charT, traits&gt; lhs,
                                  <ins>type_identity_t&lt;</ins>basic_string_view&lt;charT, traits&gt;<ins>&gt;</ins> rhs) noexcept;
</pre>
<blockquote>
<p>
-3- Let <code>R</code> denote the type <code>traits::comparison_category</code> if that <i>qualified-id</i> is valid and denotes a
type (13.10.3 <a href="https://wg21.link/temp.deduct">[temp.deduct]</a>), otherwise <code>R</code> is <code>weak_ordering</code>.
<p/>
-4- <i>Mandates</i>: <code>R</code> denotes a comparison category type (17.12.2 <a href="https://wg21.link/cmp.categories">[cmp.categories]</a>).
<p/>
-5- <i>Returns</i>: <code>static_cast&lt;R&gt;(lhs.compare(rhs) &lt;=&gt; 0)</code>.
<p/>
<ins> [<i>Note</i>: The usage of <code>type_identity_t</code> as parameter ensures that an object of type 
<code>basic_string_view&lt;charT, traits&gt;</code> can always be compared with an object of a type <code>T</code> 
with an implicit conversion to <code>basic_string_view&lt;charT, traits&gt;</code>, and vice versa, as per 
12.2.2.3 <a href="https://wg21.link/over.match.oper">[over.match.oper]</a>. &mdash; <i>end note</i>]</ins>
</p>
</blockquote>
</blockquote>

</li>

</ol>





</body>
</html>
