<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 2553: [fund.ts.v2] basic_string_view substring constructor</title>
<meta property="og:title" content="Issue 2553: [fund.ts.v2] basic_string_view substring constructor">
<meta property="og:description" content="C++ library issue. Status: NAD">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue2553.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="2553"><a href="lwg-closed.html#2553">2553</a>. [fund.ts.v2] <code>basic_string_view</code> substring constructor</h3>
<p><b>Section:</b> 7.3 <a href="https://cplusplus.github.io/fundamentals-ts/v2.html#string.view.cons">[fund.ts.v2::string.view.cons]</a> <b>Status:</b> <a href="lwg-active.html#NAD">NAD</a>
 <b>Submitter:</b> Evan Teran <b>Opened:</b> 2015-10-29 <b>Last modified:</b> 2018-06-23</p>
<p><b>Priority: </b>Not Prioritized
</p>
<p><b>View all issues with</b> <a href="lwg-status.html#NAD">NAD</a> status.</p>
<p><b>Discussion:</b></p>
<p><b>Addresses: fund.ts.v2</b></p>
<p>
<code>string_view</code> can be tremendously useful for dealing with sub-strings without copying. However, the 
<a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n3921.html">current proposal</a> for <code>basic_string_view</code>, 
has no constructor which provides a direct way of creating a view of a sub-string of a <code>basic_string</code>. 
Instead, we construct a view of the whole <code>basic_string</code>, and then as a second step create a sub-string, 
for example using <code>substr</code>. To simplify what I believe to be a common use case, I suggest adding an additional constructor.
<p/>
The proposed wording for this is as follows:
</p>
<blockquote>
<pre>
template &lt;class Allocator&gt;
basic_string_view(const basic_string&lt;charT, traits, Allocator&gt;&amp; str, size_type pos, size_type count = npos);
</pre>
<blockquote>
<p>
<i>Throws</i>: <code>out_of_range</code> if <code>pos &gt;= str.size()</code>.
<p/>
<i>Effects</i>: Determines the effective length <code>rlen</code> of the string to reference as the smaller of <code>count</code> and 
<code>size() - pos</code>.
<p/>
<i>Postcondition</i>:
</p>
<blockquote>
<table border="1">
<tr>
<td>
<code>data_ = str.data() + pos</code>
</td>
<td>
<code>size_ = rlen</code>
</td>
</tr>
</table>
</blockquote>
</blockquote>
</blockquote>
<p>
In other words, the result is as if constructed via: <code>basic_string_view(basic_string_view(str).substr(pos, count));</code>
<p/>
An example implementation could look like this:
</p>
<blockquote><pre>
template &lt;class Allocator&gt;
basic_string_view(const basic_string&lt;charT, traits, Allocator&gt;&amp; str, size_type pos, size_type count = npos) 
  : data_(nullptr), size_(0) 
{
  basic_string_view(str).substr(pos, count).swap(*this);
}
</pre></blockquote>
<p>
Note that while we have a default parameter for <code>count</code>, <code>pos</code> does not. I believe that it is best to have 
this as a separate overload, as opposed to default parameters on the current constructor for two reasons:
</p>
<ol>
<li><p>The current constructor taking a <code>basic_string</code> does not throw, this overload can throw if <code>pos &gt;= str.size()</code>.</p></li>
<li><p>This constructor performs slightly more work, it is not necessary to impose this extra work on the basic case of 
constructing a view of a whole string.</p></li>
</ol>
<p>
This has been briefly discussed in the <a href="https://groups.google.com/a/isocpp.org/forum/#!topic/std-proposals/ZtYnPdK9vIY">isocpp forums</a>. 
There were no obvious objections to this small improvement. Additionally, another reason to consider this addition is 
to provide a more consistent interface. With raw strings, we have the ability to construct a <code>basic_string_view</code> 
which is a sub-string. For example:
</p>
<blockquote><pre>
const char* s = "hello world";
auto v = string_view(s + 6);
</pre></blockquote>
<p>
But there is no constructor which easily does the same when starting with a <code>basic_string</code>.
<p/>
Finally, As a example, consider the following (trivial) code:
</p>
<blockquote><pre>
void print_string(string_view v) {
  std::cout &lt;&lt; v &lt;&lt; '\n';
}

int main() {
  std::string s = "hello world"; // for example, we want to print the sub-string "world", without copies

  // current method:
  print_substring(string_view(s).substr(6));

  // suggested method:
  print_substring(string_view(s, 6);
}
</pre></blockquote>

<p><strong>Previous resolution [SUPERSEDED]:</strong></p>
<blockquote class="note">
<p>
This wording is relative to <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/n4529.html">N4529</a>.
</p>

<ol>
<li><p>Insert between 7.3 <a href="https://cplusplus.github.io/fundamentals-ts/v2.html#string.view.cons">[fund.ts.v2::string.view.cons]</a> p5 and p6 the following sequence of paragraphs:</p>

<blockquote>
<pre>
<ins>template &lt;class Allocator&gt;
basic_string_view(const basic_string&lt;charT, traits, Allocator&gt;&amp; str, size_type pos, size_type count = npos);</ins>
</pre>
<blockquote>
<p>
<ins>-?- <i>Throws</i>: <code>out_of_range</code> if <code>pos &gt;= str.size()</code>.</ins>
<p/>
<ins>-?- <i>Effects</i>: Determines the effective length <code>rlen</code> of the string to reference as the smaller of <code>count</code> and 
<code>size() - pos</code>.</ins>
<p/>
<ins>-?- <i>Postcondition</i>: Constructs a <code>basic_string_view</code>, with the postconditions in Table ?</ins>
</p>
<blockquote>
<table border="1">
<caption><ins>Table ? &mdash; <code>basic_string_view(const basic_string&lt;charT, traits, Allocator&gt;&amp;, size_type, size_type)</code> effects</ins></caption>
<tr>
<th><ins>Element</ins></th>
<th><ins>Value</ins></th>
</tr>

<tr>
<td>
<ins><code>data_</code></ins>
</td>
<td>
<ins><code>str.data() + pos</code></ins>
</td>
</tr>

<tr>
<td>
<ins><code>size_</code></ins>
</td>
<td>
<ins><code>rlen</code></ins>
</td>
</tr>

</table>
</blockquote>
</blockquote>
</blockquote>
</li>
</ol>

</blockquote>

<p><i>[2016-03, Jacksonville]</i></p>

Change status to "LEWG"

<p>
LEWG: Do we want this constructor?
<p/>
SF F N A  SA
<p/>
0  3 2 13 1
</p>


<p id="res-2553"><b>Proposed resolution:</b></p>
<p>
Not a defect. The LWG believes this missing feature is not sufficiently serious to constitute a defect.
</p>





</body>
</html>
