<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 2757: std::string{}.insert(3, "ABCDE", 0, 1) is ambiguous</title>
<meta property="og:title" content="Issue 2757: std::string{}.insert(3, &quot;ABCDE&quot;, 0, 1) is ambiguous">
<meta property="og:description" content="C++ library issue. Status: Resolved">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue2757.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="2757"><a href="lwg-defects.html#2757">2757</a>. <code>std::string{}.insert(3, "ABCDE", 0, 1)</code> is ambiguous</h3>
<p><b>Section:</b> 27.4.3.7.4 <a href="https://wg21.link/string.insert">[string.insert]</a> <b>Status:</b> <a href="lwg-active.html#Resolved">Resolved</a>
 <b>Submitter:</b> Marshall Clow <b>Opened:</b> 2016-07-30 <b>Last modified:</b> 2020-09-06</p>
<p><b>Priority: </b>1
</p>
<p><b>View all other</b> <a href="lwg-index.html#string.insert">issues</a> in [string.insert].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#Resolved">Resolved</a> status.</p>
<p><b>Discussion:</b></p>
<p>
Before C++17, we had the following signature to <code>std::basic_string</code>:
</p>
<blockquote><pre>
basic_string&amp;
  insert(size_type pos1, const basic_string&amp; str, size_type pos2, size_type n = npos);
</pre></blockquote>
<p>
Unlike most of the other member functions on <code>std::basic_string</code>, there were not corresponding
versions that take a <code>charT*</code> or <code>(charT *, size)</code>.
<p/>
In <a href="https://wg21.link/p0254r2">p0254r2</a>, we added:
</p>
<blockquote><pre>
basic_string&amp;
  insert(size_type pos1, basic_string_view&lt;charT, traits&gt; sv, size_type pos2, size_type n = npos);
</pre></blockquote>
<p>
which made the code above ambiguous. There are two conversions from "<code>const charT*</code>", 
one to <code>basic_string</code>, and the other to <code>basic_string_view</code>, and they're both equally 
good (in the view of the compiler).
<p/>
This ambiguity also occurs with the calls
</p>
<blockquote><pre>
assign(const basic_string&amp; str,             size_type pos, size_type n = npos);
assign(basic_string_view&lt;charT, traits&gt; sv, size_type pos, size_type n = npos);
</pre></blockquote>
<p>
but I will file a separate issue (<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>) for that.
<p/>
A solution is to add <em>even more</em> overloads to <code>insert</code>, to make it match all the other member
functions of <code>basic_string</code>, which come in fours (<code>string</code>, <code>pointer</code>, <code>pointer + size</code>, 
<code>string_view</code>).
</p>

<p><i>[2016-08-03, Chicago, Robert Douglas provides wording]</i></p>


<p><strong>Previous resolution [SUPERSEDED]:</strong></p>
<blockquote class="note">
<p>This wording is relative to N4606.</p>

<ol>
<li><p>In 27.4.3 <a href="https://wg21.link/basic.string">[basic.string]</a> modify the synopsis for <code>basic_string</code> as follows:</p>

<blockquote>
<pre>
namespace std {
  template&lt;class charT, class traits = char_traits&lt;charT&gt;,
    class Allocator = allocator&lt;charT&gt;&gt;
  class basic_string {
  public:
    [&hellip;]
    <ins>template&lt;class T&gt;</ins>
    basic_string&amp; insert(size_type pos1, <del>basic_string_view&lt;charT, traits&gt;</del><ins>T</ins> sv,
                         size_type pos2, size_type n = npos);
    [&hellip;]
  };
}
</pre>
</blockquote>
</li>

<li><p>In 27.4.3.7.4 <a href="https://wg21.link/string.insert">[string.insert]</a>, modify <code>basic_string_view</code> overload as follows:</p>

<blockquote>
<pre>
<ins>template&lt;class T&gt;</ins>
basic_string&amp; insert(size_type pos1, <del>basic_string_view&lt;charT, traits&gt;</del><ins>T</ins> sv,
                     size_type pos2, size_type n = npos);
</pre>
<blockquote>
<p>
[&hellip;]
<p/>
<ins>-?- <i>Remarks:</i> This function shall not participate in overload resolution unless
<code>is_same_v&lt;T, basic_string_view&lt;charT, traits&gt;&gt;</code> is <code>true</code>.</ins>
</p>
</blockquote>
</blockquote>
</li>
</ol>
</blockquote>

<p><i>[2016-08-04, Chicago, Robert Douglas comments]</i></p>

<p>
For the sake of simplicity, the previous wording suggestion has been merged into the proposed wording
of LWG <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>.
</p>

<p><i>[08-2016, Chicago]</i></p>

<p>Fri PM: Move to Tentatively Ready (along with <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>).</p>

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

<p>Since <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> has been moved back to Open, move this one, too</p>

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

<p>Ville's wording for <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> has been implemented in libstdc++ and libc++. Move <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> to Tentatively Ready and this one to Tentatively Resolved</p>


<p id="res-2757"><b>Proposed resolution:</b></p>
<p>
This issue is resolved by the proposed wording for LWG <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>.
</p>





</body>
</html>
