<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 2372: Assignment from int to std::string</title>
<meta property="og:title" content="Issue 2372: Assignment from int to std::string">
<meta property="og:description" content="C++ library issue. Status: NAD">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue2372.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="2372"><a href="lwg-closed.html#2372">2372</a>. Assignment from int to <code>std::string</code></h3>
<p><b>Section:</b> 27.4.3 <a href="https://wg21.link/basic.string">[basic.string]</a> <b>Status:</b> <a href="lwg-active.html#NAD">NAD</a>
 <b>Submitter:</b> Andrzej Krzemie&#324;ski <b>Opened:</b> 2014-03-13 <b>Last modified:</b> 2018-06-23</p>
<p><b>Priority: </b>4
</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#NAD">NAD</a> status.</p>
<p><b>Discussion:</b></p>
<p>
The following code works in C++:
</p>
<blockquote><pre>
int i = 300;
std::string threeHundred;
threeHundred = i;
</pre></blockquote>
<p>
"Works" == "Compiles and doesn't have an undefined behavior". But it may not be obvious and in fact misleading what it does. 
This assignment converts an <code>int</code> to <code>char</code> and then uses <code>string</code>'s assignment from <code>char</code>. While 
the assignment from <code>char</code> can be considered a feature, being able to assign from an int looks like a safety gap. Someone 
may believe C++ works like "dynamically typed" languages and expect a lexical conversion to take place.
<p/>
Ideally the assignment from <code>char</code> could be deprecated and later removed, but as a less intrusive alternative one could 
consider adding a SFINAEd deleted function template:
</p>
<blockquote><pre>
template &lt;typename IntT&gt; // enable if is_integral&lt;IntT&gt;::value
basic_string&amp; operator=(IntT) = delete;
</pre></blockquote>

<p><i>[Lenexa 2015-06-06: Move to LEWG]</i></p>

<p>RS: <code>std::string x('0' + n);</code> broken by this.</p>
<p>MC: This is an extension, move to LEWG.</p>
<p>Move to LEWG, consensus.</p>

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

<ol>
<li><p>To 27.4.3 <a href="https://wg21.link/basic.string">[basic.string]</a>, class template <code>basic_string</code> synopsis, add as indicated:</p>

<blockquote><pre>
basic_string&amp; operator=(const basic_string&amp; str);
basic_string&amp; operator=(basic_string&amp;&amp; str) noexcept;
basic_string&amp; operator=(const charT* s);
basic_string&amp; operator=(charT c);
<ins>template &lt;class IntT&gt; basic_string&amp; operator=(IntT i) = delete;</ins>
basic_string&amp; operator=(initializer_list&lt;charT&gt;);
</pre></blockquote>
</li>

<li><p>Add after 27.4.3.3 <a href="https://wg21.link/string.cons">[string.cons]</a> p26 as indicated:</p>

<blockquote>
<pre>
basic_string&amp; operator=(charT c);
</pre>
<blockquote>
<p>
-26- <i>Returns</i>: <code>*this = basic_string(1,c)</code>.
</p>
</blockquote>

<pre>
<ins>template &lt;class IntT&gt; basic_string&amp; operator=(IntT i) = delete;</ins>
</pre>
<blockquote>
<p>
<ins>-?- <i>Remarks</i>: This signature shall not participate in overload resolution unless <code>is_integral&lt;T&gt;::value</code> is 
<code>true</code>.</ins>
</p>
</blockquote>
</blockquote>

</li>

</ol>
</blockquote>

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

<p>
<code>is_integral&lt;T&gt;::value</code> &rarr; <code>is_arithmetic&lt;<i>tmpl-arg</i>&gt;::value</code>
<p/>
This needs a paper; close the issue
<p/>
We don't think the breakage is acceptable.
<p/>
Guidance to author: Look for a way to encourage a warning; discomfort with calling that "deprecation".
<p/>
Consider <code>+=</code> and <code>push_back</code>.
</p>


<p id="res-2372"><b>Proposed resolution:</b></p>
<p>
This should be addressed by a paper addressed to LEWG.
</p>





</body>
</html>
