<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 3075: basic_string needs deduction guides from basic_string_view</title>
<meta property="og:title" content="Issue 3075: basic_string needs deduction guides from basic_string_view">
<meta property="og:description" content="C++ library issue. Status: C++20">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue3075.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++20">C++20</a> status.</em></p>
<h3 id="3075"><a href="lwg-defects.html#3075">3075</a>. <code>basic_string</code> needs deduction guides from <code>basic_string_view</code></h3>
<p><b>Section:</b> 27.4.3 <a href="https://wg21.link/basic.string">[basic.string]</a>, 27.4.3.3 <a href="https://wg21.link/string.cons">[string.cons]</a> <b>Status:</b> <a href="lwg-active.html#C++20">C++20</a>
 <b>Submitter:</b> Stephan T. Lavavej <b>Opened:</b> 2018-03-03 <b>Last modified:</b> 2021-02-25</p>
<p><b>Priority: </b>Not Prioritized
</p>
<p><b>View other</b> <a href="lwg-index-open.html#basic.string">active issues</a> in [basic.string].</p>
<p><b>View all other</b> <a href="lwg-index.html#basic.string">issues</a> in [basic.string].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#C++20">C++20</a> status.</p>
<p><b>Discussion:</b></p>
<p>
The Proposed Resolution for LWG <a href="lwg-defects.html#2946" title="LWG 2758's resolution missed further corrections (Status: C++20)">2946</a><sup><a href="https://cplusplus.github.io/LWG/issue2946" title="Latest snapshot">(i)</a></sup> appears to be correct and we've implemented it in MSVC, 
but it worsens a pre-existing problem with basic_string class template argument deduction.
<p/>
The following <code>s1</code> and <code>s2</code> compiled in C++17 before LWG 2946's PR, fail 
to compile after LWG 2946's PR, and are fixed by my PR:</p>
<blockquote><pre>
basic_string s1("cat"sv);
basic_string s2("cat"sv, alloc);
</pre></blockquote>
<p>
The following s4 failed to compile in C++17, and is fixed by my PR:
</p>
<blockquote><pre>
// basic_string s3("cat"sv, 1, 1);
basic_string s4("cat"sv, 1, 1, alloc);
</pre></blockquote>
<p>
(s3 failed to compile in C++17, and would be fixed by my PR, but it is affected by a pre-existing and 
unrelated ambiguity which I am not attempting to fix here.)
<p/>
As C++17 and LWG 2946's PR introduced templated constructors for <code>basic_string</code> from 
<code>basic_string_view</code>, we need to add corresponding deduction guides.
<p/>
The constructors take <code>const T&amp;</code> that's convertible to <code>basic_string_view</code> (the 
additional constraint about not converting to <code>const charT*</code> is irrelevant here). However, CTAD 
can't deduce <code>charT</code> and <code>traits</code> from arbitrary user-defined types, so the deduction guides 
need <code>T</code> to be exactly <code>basic_string_view</code>.
<p/>
Additionally, we need to handle the <code>size_type</code> parameters in the same way that the unordered 
containers do. This PR has been implemented in MSVC.
</p>

<p><i>[2018-14: Wednesday night issues processing: both this and <a href="lwg-defects.html#2946" title="LWG 2758's resolution missed further corrections (Status: C++20)">2946</a><sup><a href="https://cplusplus.github.io/LWG/issue2946" title="Latest snapshot">(i)</a></sup> to status "Immediate".]</i></p>

<p><i>[2018-3-17 Adopted in Jacksonville]</i></p>



<p id="res-3075"><b>Proposed resolution:</b></p>
<p>
This wording is relative to <a href="https://wg21.link/n4727">N4727</a>.
</p>

<ol>
<li>
<p>Edit 27.4.3 <a href="https://wg21.link/basic.string">[basic.string]</a>, class template <code>basic_string</code> synopsis, as indicated:</p>
<blockquote>
<blockquote>
<pre>
[&hellip;]

template&lt;class InputIterator,
         class Allocator = allocator&lt;typename iterator_traits&lt;InputIterator&gt;::value_type&gt;&gt;
  basic_string(InputIterator, InputIterator, Allocator = Allocator())
    -&gt; basic_string&lt;typename iterator_traits&lt;InputIterator&gt;::value_type,
                    char_traits&lt;typename iterator_traits&lt;InputIterator&gt;::value_type&gt;,
                     Allocator&gt;;

<ins>template&lt;class charT,
         class traits,
         class Allocator = allocator&lt;charT&gt;&gt;
  explicit basic_string(basic_string_view&lt;charT, traits&gt;, const Allocator&amp; = Allocator())
    -&gt; basic_string&lt;charT, traits, Allocator&gt;;

template&lt;class charT,
         class traits,
         class Allocator = allocator&lt;charT&gt;&gt;
  basic_string(basic_string_view&lt;charT, traits&gt;, typename <i>see below</i>::size_type, typename <i>see below</i>::size_type, 
               const Allocator&amp; = Allocator())
    -&gt; basic_string&lt;charT, traits, Allocator&gt;;</ins>

}                     
</pre></blockquote>
<p>
<ins>-?- A <code>size_type</code> parameter type in a <code>basic_string</code> deduction guide refers to the 
<code>size_type</code> member type of the type deduced by the deduction guide.</ins>
</p>
</blockquote>
</li>

<li>
<p>Edit 27.4.3.3 <a href="https://wg21.link/string.cons">[string.cons]</a> as indicated:</p>
<blockquote>
<pre>
template&lt;class InputIterator,
         class Allocator = allocator&lt;typename iterator_traits&lt;InputIterator>::value_type&gt;&gt;
  basic_string(InputIterator, InputIterator, Allocator = Allocator())
    -&gt; basic_string&lt;typename iterator_traits&lt;InputIterator&gt;::value_type,
                    char_traits&lt;typename iterator_traits&lt;InputIterator&gt;::value_type&gt;,
                     Allocator&gt;;
</pre>
<blockquote>
<p>
-25- <i>Remarks:</i> Shall not participate in overload resolution if <code>InputIterator</code> is a type 
that does not qualify as an input iterator, or if <code>Allocator</code> is a type that does not qualify as an allocator (23.2.2 <a href="https://wg21.link/container.requirements.general">[container.requirements.general]</a>).
</p>
</blockquote>
<pre>
<ins>template&lt;class charT,
         class traits,
         class Allocator = allocator&lt;charT&gt;&gt;
  explicit basic_string(basic_string_view&lt;charT, traits&gt;, const Allocator&amp; = Allocator())
    -&gt; basic_string&lt;charT, traits, Allocator&gt;;

template&lt;class charT,
         class traits,
         class Allocator = allocator&lt;charT&gt;&gt;
  basic_string(basic_string_view&lt;charT, traits&gt;, typename <i>see below</i>::size_type, typename <i>see below</i>::size_type, 
               const Allocator&amp; = Allocator())
    -&gt; basic_string&lt;charT, traits, Allocator&gt;;</ins>                                          
</pre>
<blockquote>
<p>
<ins>-?- <i>Remarks:</i> Shall not participate in overload resolution if <code>Allocator</code> is a type 
that does not qualify as an allocator (23.2.2 <a href="https://wg21.link/container.requirements.general">[container.requirements.general]</a>).</ins>
</p>
</blockquote>
</blockquote>
</li>

</ol>





</body>
</html>
