<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 466: basic_string ctor should prevent null pointer error</title>
<meta property="og:title" content="Issue 466: basic_string ctor should prevent null pointer error">
<meta property="og:description" content="C++ library issue. Status: NAD">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue466.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="466"><a href="lwg-closed.html#466">466</a>. <code>basic_string</code> ctor should prevent null pointer error</h3>
<p><b>Section:</b> 27.4.3.2 <a href="https://wg21.link/string.require">[string.require]</a> <b>Status:</b> <a href="lwg-active.html#NAD">NAD</a>
 <b>Submitter:</b> Daniel Frey <b>Opened:</b> 2004-06-10 <b>Last modified:</b> 2016-01-28</p>
<p><b>Priority: </b>Not Prioritized
</p>
<p><b>View all other</b> <a href="lwg-index.html#string.require">issues</a> in [string.require].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#NAD">NAD</a> status.</p>
<p><b>Discussion:</b></p>
<p>
Today, my colleagues and me wasted a lot of time. After some time, I
found the problem. It could be reduced to the following short example:
</p>

<pre>
  #include &lt;string&gt;
  int main() { std::string( 0 ); }
</pre>

<p>The problem is that the tested compilers (GCC 2.95.2, GCC 3.3.1 and
Comeau online) compile the above without errors or warnings! The
programs (at least for the GCC) resulted in a SEGV.</p>

<p>I know that the standard explicitly states that the ctor of string
requires a <code>char*</code> which is not zero. STLs could easily detect the above
case with a private ctor for <code>basic_string</code> which takes a single '<code>int</code>'
argument. This would catch the above code at compile time and would not
ambiguate any other legal ctors.</p>

<p><i>[Redmond: No great enthusiasm for doing this.  If we do,
  however, we want to do it for all places that take <code>charT*</code>
  pointers, not just the single-argument constructor.  The other
  question is whether we want to catch this at compile time (in which
  case we catch the error of a literal 0, but not an expression whose
  value is a null pointer), at run time, or both.
  Recommend NAD.  Relegate this functionality to debugging implementations.]</i></p>


<p><i>[
Post Summit: Alisdair requests this be re-opened as several new language facilities are
designed to solve exactly this kind of problem.
]</i></p>


<p><i>[
Batavia (2009-05):
]</i></p>

<blockquote><p>
We are unable to achieve consensus on an approach to a resolution.
There is some sentiment for treating this as a QOI matter.
It is also possible
that when <code>string</code> is brought into the concepts world,
this issue might be addressed in that context.
</p></blockquote>

<p><i>[
2009-07 Frankfurt
]</i></p>


<blockquote>
<p>
We considered three options:
</p>

<ul>
<li>The proposed resolution.</li>
<li>NAD</li>
<li>Interpret a null pointer as the empty string.</li>
</ul>

<p>
The consensus was NAD.
</p>
</blockquote>



<p id="res-466"><b>Proposed resolution:</b></p>
<p>
Add to the synopsis in 27.4.3 <a href="https://wg21.link/basic.string">[basic.string]</a>
</p>

<blockquote><pre>
<ins>basic_string( nullptr_t ) = delete;</ins>
</pre></blockquote>





</body>
</html>
