<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 1103: system_error constructor postcondition overly strict</title>
<meta property="og:title" content="Issue 1103: system_error constructor postcondition overly strict">
<meta property="og:description" content="C++ library issue. Status: C++11">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue1103.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++11">C++11</a> status.</em></p>
<h3 id="1103"><a href="lwg-defects.html#1103">1103</a>. <code>system_error</code> constructor postcondition overly strict</h3>
<p><b>Section:</b> 19.5.8.2 <a href="https://wg21.link/syserr.syserr.members">[syserr.syserr.members]</a> <b>Status:</b> <a href="lwg-active.html#C++11">C++11</a>
 <b>Submitter:</b> Howard Hinnant <b>Opened:</b> 2009-04-25 <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#syserr.syserr.members">issues</a> in [syserr.syserr.members].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#C++11">C++11</a> status.</p>
<p><b>Discussion:</b></p>
<p>
19.5.8.2 <a href="https://wg21.link/syserr.syserr.members">[syserr.syserr.members]</a> says:
</p>

<blockquote><pre>
system_error(error_code ec, const string&amp; what_arg);
</pre>
<blockquote>
<p>
<i>Effects:</i> Constructs an object of class <code>system_error</code>.
</p>
<p>
<i>Postconditions:</i> <code>code() == ec</code> and <code>strcmp(runtime_error::what(), what_arg.c_str()) == 0</code>.
</p>
</blockquote>
</blockquote>

<p>
However the intent is for:
</p>

<blockquote><pre>
std::system_error se(std::errc::not_a_directory, "In FooBar");
...
se.what();  <span style="color:#C80000">// returns something along the lines of:</span>
            <span style="color:#C80000">//   "In FooBar: Not a directory"</span>
</pre></blockquote>

<p>
The way the constructor postconditions are set up now, to achieve both
conformance, and the desired intent in the <code>what()</code> string, the
<code>system_error</code> constructor must store "In FooBar" in the base class,
and then form the desired output each time <code>what()</code> is called.  Or
alternatively, store "In FooBar" in the base class, and store the desired
<code>what()</code> string in the derived <code>system_error</code>, and override
<code>what()</code> to return the string in the derived part.
</p>

<p>
Both of the above implementations seem suboptimal to me.  In one I'm computing
a new string every time <code>what()</code> is called.  And since <code>what()</code>
can't propagate exceptions, the client may get a different string on different
calls.
</p>

<p>
The second solution requires storing two strings instead of one.
</p>

<p>
What I would like to be able to do is form the desired <code>what()</code> string
once in the <code>system_error</code> constructor, and store <em>that</em> in the
base class.  Now I'm:
</p>

<ol>
<li>Computing the desired <code>what()</code> only once.</li>
<li>The base class <code>what()</code> definition is sufficient and nothrow.</li>
<li>I'm not storing multiple strings.</li>
</ol>

<p>
This is smaller code, smaller data, and faster.
</p>

<p>
<code>ios_base::failure</code> has the same issue.
</p>

<p><i>[
Comments about this change received favorable comments from the <code>system_error</code>
designers.
]</i></p>


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


<blockquote>
<p>
We agree with the proposed resolution.
</p>
<p>
Move to Tentatively Ready.
</p>
</blockquote>


<p id="res-1103"><b>Proposed resolution:</b></p>
<p>
In 19.5.8.2 <a href="https://wg21.link/syserr.syserr.members">[syserr.syserr.members]</a>, change the following constructor postconditions:
</p>

<blockquote>
<pre>
system_error(error_code ec, const string&amp; what_arg);
</pre>
<blockquote><p>
-2- <i>Postconditions:</i> <code>code() == ec</code>
and <code><del>strcmp(runtime_error::what(), what_arg.c_str()) == 0</del>
<ins>string(what()).find(what_arg) != string::npos</ins></code>.
</p></blockquote>

<pre>
system_error(error_code ec, const char* what_arg);
</pre>
<blockquote><p>
-4- <i>Postconditions:</i> <code>code() == ec</code>
and <code><del>strcmp(runtime_error::what(), what_arg) == 0</del>
<ins>string(what()).find(what_arg) != string::npos</ins></code>.
</p></blockquote>

<pre>
system_error(error_code ec);
</pre>
<blockquote><p>
-6- <i>Postconditions:</i> <code>code() == ec</code>
<del>and <code>strcmp(runtime_error::what(), ""</code></del>.
</p></blockquote>

<pre>
system_error(int ev, const error_category&amp; ecat, const string&amp; what_arg);
</pre>
<blockquote><p>
-8- <i>Postconditions:</i> <code>code() == error_code(ev, ecat)</code>
and <code><del>strcmp(runtime_error::what(), what_arg.c_str()) == 0</del>
<ins>string(what()).find(what_arg) != string::npos</ins></code>.
</p></blockquote>

<pre>
system_error(int ev, const error_category&amp; ecat, const char* what_arg);
</pre>
<blockquote><p>
-10- <i>Postconditions:</i> <code>code() == error_code(ev, ecat)</code>
and <code><del>strcmp(runtime_error::what(), what_arg) == 0</del>
<ins>string(what()).find(what_arg) != string::npos</ins></code>.
</p></blockquote>

<pre>
system_error(int ev, const error_category&amp; ecat);
</pre>
<blockquote><p>
-12- <i>Postconditions:</i> <code>code() == error_code(ev, ecat)</code>
<del>and <code>strcmp(runtime_error::what(), "") == 0</code></del>.
</p></blockquote>

</blockquote>

<p>
In 19.5.8.2 <a href="https://wg21.link/syserr.syserr.members">[syserr.syserr.members]</a>, change the description of <code>what()</code>:
</p>

<blockquote>
<pre>
const char *what() const throw();
</pre>
<blockquote>
<p>
-14- <i>Returns:</i> An NTBS incorporating <del><code>runtime_error::what()</code> and
<code>code().message()</code></del> <ins>the arguments supplied in the constructor</ins>.
</p>
<p>
[<i>Note:</i> <del>One possible implementation would be:</del>
<ins>The return NTBS might take the form: <code>what_arg + ": " + code().message()</code></ins>
</p>
<blockquote><pre><del>
if (msg.empty()) { 
  try { 
    string tmp = runtime_error::what(); 
    if (code()) { 
      if (!tmp.empty()) 
        tmp += ": "; 
      tmp += code().message(); 
    } 
    swap(msg, tmp); 
  } catch(...) { 
    return runtime_error::what(); 
  } 
return msg.c_str();
</del></pre></blockquote>
<p>
&mdash; <i>end note</i>]
</p>
</blockquote>
</blockquote>

<p>
In  [ios::failure], change the synopsis:
</p>

<blockquote><pre>
namespace std { 
  class ios_base::failure : public system_error { 
  public: 
    explicit failure(const string&amp; msg, const error_code&amp; ec = io_errc::stream); 
    explicit failure(const char* msg, const error_code&amp; ec = io_errc::stream); 
    <del>virtual const char* what() const throw();</del>
  }; 
}
</pre></blockquote>

<p>
In  [ios::failure], change the description of the constructors:
</p>

<blockquote>

<pre>
explicit failure(const string&amp; msg, , const error_code&amp; ec = io_errc::stream);
</pre>
<blockquote>
<p>
-3- <i>Effects:</i> Constructs an object of class <code>failure</code>
<ins>by constructing the base class with <code>msg</code> and <code>ec</code></ins>.
</p>
<p>
<del>-4- <i>Postcondition:</i> <code>code() == ec</code> and <code>strcmp(what(), msg.c_str()) == 0</code></del>
</p>
</blockquote>

<pre>
explicit failure(const char* msg, const error_code&amp; ec = io_errc::stream);
</pre>
<blockquote>
<p>
-5- <i>Effects:</i> Constructs an object of class <code>failure</code>
<ins>by constructing the base class with <code>msg</code> and <code>ec</code></ins>.
</p>
<p>
<del>-6- <i>Postcondition:</i> <code>code() == ec and strcmp(what(), msg) == 0</code></del>
</p>
</blockquote>

</blockquote>

<p>
In  [ios::failure], remove <code>what</code> (the base class definition
need not be repeated here).
</p>

<blockquote>
<pre>
<del>const char* what() const;</del>
</pre>
<blockquote><p>
<del>-7- <i>Returns:</i> The message <code>msg</code> with which the exception was created.</del>
</p></blockquote>

</blockquote>






</body>
</html>
