<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 2771: Broken Effects of some basic_string::compare functions in terms of basic_string_view</title>
<meta property="og:title" content="Issue 2771: Broken Effects of some basic_string::compare functions in terms of basic_string_view">
<meta property="og:description" content="C++ library issue. Status: C++17">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue2771.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="2771"><a href="lwg-defects.html#2771">2771</a>. Broken <i>Effects</i> of some <code>basic_string::compare</code> functions in terms of <code>basic_string_view</code></h3>
<p><b>Section:</b> 27.4.3.8.4 <a href="https://wg21.link/string.compare">[string.compare]</a> <b>Status:</b> <a href="lwg-active.html#C++17">C++17</a>
 <b>Submitter:</b> Daniel Kr&uuml;gler <b>Opened:</b> 2016-09-05 <b>Last modified:</b> 2017-07-30</p>
<p><b>Priority: </b>1
</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>
Some <code>basic_string::compare</code> functions are specified in terms of a non-existing <code>basic_string_view</code>
constructor, namely 27.4.3.8.4 <a href="https://wg21.link/string.compare">[string.compare]</a> p3,
</p>
<blockquote><pre>
return basic_string_view&lt;charT, traits&gt;(this.data(), pos1, n1).compare(sv);
</pre></blockquote>
<p>
and 27.4.3.8.4 <a href="https://wg21.link/string.compare">[string.compare]</a> p4:
</p>
<blockquote><pre>
return basic_string_view&lt;charT, traits&gt;(this.data(), pos1, n1).compare(sv, pos2, n2);
</pre></blockquote>
<p>
because there doesn't exist a <code>basic_string_view</code> constructor with three arguments.
<p/>
Albeit this can be easily fixed by a proper combination of the existing constructor
</p>
<blockquote><pre>
constexpr basic_string_view(const charT* str, size_type len);
</pre></blockquote>
<p>
with the additional member function
</p>
<blockquote><pre>
constexpr basic_string_view substr(size_type pos = 0, size_type n = npos) const;
</pre></blockquote>
<p>
it should be decided whether adding the seemingly natural constructor
</p>
<blockquote><pre>
constexpr basic_string_view(const charT* str, size_type pos, size_type n);
</pre></blockquote>
<p>
could simplify matters. A counter argument for this addition might be, that <code>basic_string</code>
doesn't provide this constructor either.
<p/>
Another problem is related to the specification of 27.4.3.8.4 <a href="https://wg21.link/string.compare">[string.compare]</a> p4, which attempts to
call a non-existing <code>basic_string_view::compare</code> overload that would match the signature:
</p>
<blockquote><pre>
constexpr int compare(basic_string_view str, size_type pos1, size_type n1) const;
</pre></blockquote>

<p><i>[2016-09-09 Issues Resolution Telecon]</i></p>

<p>Marshall to investigate using P/R vs. adding the missing constructor.</p>

<p><i>[2016-10 Issues Resolution Telecon]</i></p>

<p>Marshall reports that P/R is better. Status to Tentatively Ready</p>


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

<blockquote class="note">
<p>
[<i>Drafting note</i>: The wording changes below are for the same part of the standard as <a href="lwg-defects.html#2758" title="std::string{}.assign(&quot;ABCDE&quot;, 0, 1) is ambiguous (Status: C++17)">2758</a><sup><a href="https://cplusplus.github.io/LWG/issue2758" title="Latest snapshot">(i)</a></sup>. 
However, they do not conflict. This one changes the "Effects" of the routine, while the other changes the definition.
&mdash; <i>end drafting note</i>]
</p>
</blockquote>

<ol>
<li><p>Change 27.4.3.8.4 <a href="https://wg21.link/string.compare">[string.compare]</a> as indicated:</p>

<blockquote>
<pre>
int compare(size_type pos1, size_type n1,
            basic_string_view&lt;charT, traits&gt; sv) const;
</pre>
<blockquote>
<p>
-3- <i>Effects:</i> Equivalent to:
</p>
<blockquote>
<pre>
return basic_string_view&lt;charT, traits&gt;(<del>this.</del>data(), <ins>size()).substr(</ins>pos1, n1).compare(sv);
</pre>
</blockquote>
</blockquote>
<pre>
int compare(size_type pos1, size_type n1,
            basic_string_view&lt;charT, traits&gt; sv,
            size_type pos2, size_type n2 = npos) const;
</pre>
<blockquote>
<p>
-4- <i>Effects:</i> Equivalent to:
</p>
<blockquote>
<pre>
return basic_string_view&lt;charT, traits&gt;(<del>this.</del>data(), <ins>size()).substr(</ins>pos1, n1).compare(sv<del>,</del><ins>.substr(</ins>pos2, n2<ins>)</ins>);
</pre>
</blockquote>
</blockquote>
</blockquote>
</li>
</ol>






</body>
</html>
