<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 832: Applying constexpr to System error support</title>
<meta property="og:title" content="Issue 832: Applying constexpr to System error support">
<meta property="og:description" content="C++ library issue. Status: NAD">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue832.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="832"><a href="lwg-closed.html#832">832</a>. Applying constexpr to System error support</h3>
<p><b>Section:</b> 19.5 <a href="https://wg21.link/syserr">[syserr]</a> <b>Status:</b> <a href="lwg-active.html#NAD">NAD</a>
 <b>Submitter:</b> Beman Dawes <b>Opened:</b> 2008-05-14 <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">issues</a> in [syserr].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#NAD">NAD</a> status.</p>
<p><b>Discussion:</b></p>
<p>
Initialization of objects of class <code>error_code</code>
(19.5.4 <a href="https://wg21.link/syserr.errcode">[syserr.errcode]</a>) and class
<code>error_condition</code> (19.5.5 <a href="https://wg21.link/syserr.errcondition">[syserr.errcondition]</a>) can be made simpler and more reliable by use of
the new <code>constexpr</code> feature 
[<a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2349.pdf">N2349</a>]
of C++0x. Less code will need to be
generated for both library implementations and user programs when
manipulating constant objects of these types. 
</p>

<p>
This was not proposed originally because the constant expressions
proposal was moving into the standard at about the same time as the
Diagnostics Enhancements proposal 
[<a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2241.html">N2241</a>],
and it wasn't desirable to
make the later depend on the former. There were also technical concerns
as to how <code>constexpr</code> would apply to references. Those concerns are now
resolved; <code>constexpr</code> can't be used for references, and that fact is
reflected in the proposed resolution.
</p>

<p>
Thanks to Jens Maurer, Gabriel Dos Reis, and Bjarne Stroustrup for clarification of <code>constexpr</code> requirements.
</p>

<p>
LWG issue <a href="lwg-defects.html#804" title="Some problems with classes error_code/error_condition (Status: CD1)">804</a><sup><a href="https://cplusplus.github.io/LWG/issue804" title="Latest snapshot">(i)</a></sup> is related in that it raises the question of whether the
exposition only member <code>cat_</code> of class <code>error_code</code> (19.5.4 <a href="https://wg21.link/syserr.errcode">[syserr.errcode]</a>) and class
<code>error_condition</code> (19.5.5 <a href="https://wg21.link/syserr.errcondition">[syserr.errcondition]</a>) should be presented as a reference or pointer.
While in the context of <a href="lwg-defects.html#804" title="Some problems with classes error_code/error_condition (Status: CD1)">804</a><sup><a href="https://cplusplus.github.io/LWG/issue804" title="Latest snapshot">(i)</a></sup> that is arguably an editorial question,
presenting it as a pointer becomes more or less required with this
proposal, given <code>constexpr</code> does not play well with references. The
proposed resolution thus changes the private member to a pointer, which
also brings it in sync with real implementations.
</p>

<p><i>[
Sophia Antipolis:
]</i></p>


<blockquote><p>
On going question of extern pointer vs. inline functions for interface.
</p></blockquote>

<p><i>[
Pre-San Francisco:
]</i></p>


<blockquote>
<p>
Beman Dawes reports that this proposal is unimplementable, and thus NAD.
</p>
<p>
Implementation would require <code>constexpr</code> objects of classes derived
from class <code>error_category</code>, which has virtual functions, and that is
not allowed by the core language. This was determined when trying to
implement the proposal using a constexpr enabled compiler provided
by Gabriel Dos Reis, and subsequently verified in discussions with
Gabriel and Jens Maurer.
</p>

</blockquote>


<p id="res-832"><b>Proposed resolution:</b></p>
<p>
The proposed wording assumes the LWG <a href="lwg-defects.html#805" title="posix_error::posix_errno concerns (Status: CD1)">805</a><sup><a href="https://cplusplus.github.io/LWG/issue805" title="Latest snapshot">(i)</a></sup> proposed wording has been
applied to the WP, resulting in the former <code>posix_category</code> being renamed
<code>generic_category</code>. If <a href="lwg-defects.html#805" title="posix_error::posix_errno concerns (Status: CD1)">805</a><sup><a href="https://cplusplus.github.io/LWG/issue805" title="Latest snapshot">(i)</a></sup> has not been applied, the names in this
proposal must be adjusted accordingly.
</p>

<p>
Change 19.5.3.1 <a href="https://wg21.link/syserr.errcat.overview">[syserr.errcat.overview]</a> Class
<code>error_category</code> overview <code>error_category</code> synopsis  as
indicated:
</p>

<blockquote><pre>
<del>const error_category&amp; get_generic_category();</del>
<del>const error_category&amp; get_system_category();</del>

<del>static</del> <ins>extern</ins> const error_category<del>&amp;</del><ins>* const</ins> generic_category<del> = get_generic_category()</del>;
<del>static</del> <ins>extern</ins> const error_category<del>&amp;</del><ins>* const</ins> <del>native_category</del> system_category<del> = get_system_category()</del>;
</pre></blockquote>

<p>
Change 19.5.3.5 <a href="https://wg21.link/syserr.errcat.objects">[syserr.errcat.objects]</a> Error category objects as indicated:
</p>

<blockquote>
<pre>
<ins>extern</ins> const error_category<del>&amp;</del><ins>* const</ins> <del>get_</del>generic_category<del>()</del>;
</pre>
<p>
<del><i>Returns:</i> A reference</del> <ins><code>generic_category</code> shall point</ins>
to <del>an</del> <ins>a statically initialized</ins> object of a type derived from
class <code>error_category</code>.
</p>

<p>
<del><i>Remarks:</i></del> The object's <code>default_error_condition</code> and <code>equivalent</code> virtual
functions shall behave as specified for the class <code>error_category</code>. The
object's <code>name</code> virtual function shall return a pointer to the string
<code>"GENERIC"</code>.
</p>

<pre>
<ins>extern</ins> const error_category<del>&amp;</del><ins>* const</ins> <del>get_</del>system_category<del>()</del>;
</pre>

<p>
<del><i>Returns:</i> A reference</del> <ins><code>system_category</code> shall point</ins>
to <del>an</del> <ins>a statically
initialized</ins> object of a type derived from class <code>error_category</code>.
</p>

<p>
<del><i>Remarks:</i></del>  The object's <code>equivalent</code> virtual functions shall behave as
specified for class <code>error_category</code>. The object's <code>name</code> virtual function
shall return a pointer to the string <code>"system"</code>. The object's
<code>default_error_condition</code> virtual function shall behave as follows:
</p>

<p>
If the argument <code>ev</code> corresponds to a POSIX <code>errno</code> value <code>posv</code>, the function
shall return <code>error_condition(posv, generic_category)</code>. Otherwise, the
function shall return <code>error_condition(ev, system_category)</code>. What
constitutes correspondence for any given operating system is
unspecified. [<i>Note:</i> The number of potential system error codes is large
and unbounded, and some may not correspond to any POSIX <code>errno</code> value.
Thus implementations are given latitude in determining correspondence.
<i>-- end note</i>]
</p>
</blockquote>

<p>
Change 19.5.4.1 <a href="https://wg21.link/syserr.errcode.overview">[syserr.errcode.overview]</a> Class <code>error_code</code> overview as indicated:
</p>

<blockquote><pre>
class error_code {
public:
  ...;
  <ins>constexpr</ins> error_code(int val, const error_category<del>&amp;</del><ins>*</ins> cat);
  ...
  void assign(int val, const error_category<del>&amp;</del><ins>*</ins> cat);
  ...
  const error_category<del>&amp;</del><ins>*</ins> category() const;
  ...
private:
  int val_;                    // exposition only
  const error_category<del>&amp;</del><ins>*</ins> cat_; // exposition only
</pre></blockquote>

<p>
Change 19.5.4.2 <a href="https://wg21.link/syserr.errcode.constructors">[syserr.errcode.constructors]</a> Class <code>error_code</code> constructors as indicated:
</p>

<blockquote>
<pre>
<ins>constexpr</ins> error_code(int val, const error_category<del>&amp;</del><ins>*</ins> cat);
</pre>
<p>
<i>Effects:</i> Constructs an object of type <code>error_code</code>.
</p>
<p>
<i>Postconditions:</i> <code>val_ == val</code> and <code>cat_ == cat</code>.
</p>
<p>
<i>Throws:</i> Nothing.
</p>
</blockquote>

<p>
Change 19.5.4.3 <a href="https://wg21.link/syserr.errcode.modifiers">[syserr.errcode.modifiers]</a> Class <code>error_code</code> modifiers  as indicated:
</p>

<blockquote>
<pre>
void assign(int val, const error_category<del>&amp;</del><ins>*</ins> cat);
</pre>
<p>
<i>Postconditions:</i> <code>val_ == val</code> and <code>cat_ == cat</code>.
</p>
<p>
<i>Throws:</i> Nothing.
</p>
</blockquote>

<p>
Change 19.5.4.4 <a href="https://wg21.link/syserr.errcode.observers">[syserr.errcode.observers]</a> Class <code>error_code</code> observers  as indicated:
</p>

<blockquote>
<pre>
const error_category<del>&amp;</del><ins>*</ins> category() const;
</pre>

<p>
<i>Returns:</i> <code>cat_</code>.
</p>
<p>
<i>Throws:</i> Nothing.
</p>
</blockquote>

<p>
Change 19.5.5.1 <a href="https://wg21.link/syserr.errcondition.overview">[syserr.errcondition.overview]</a> Class <code>error_condition</code> overview   as indicated:
</p>

<blockquote>
<pre>
class error_condition {
public:
  ...;
  <ins>constexpr</ins> error_condition(int val, const error_category<del>&amp;</del><ins>*</ins> cat);
  ...
  void assign(int val, const error_category<del>&amp;</del><ins>*</ins> cat);
  ...
  const error_category<del>&amp;</del><ins>*</ins> category() const;
  ...
private:
  int val_;                    // exposition only
  const error_category<del>&amp;</del><ins>*</ins> cat_; // exposition only
</pre>
</blockquote>

<p>
Change 19.5.5.2 <a href="https://wg21.link/syserr.errcondition.constructors">[syserr.errcondition.constructors]</a> Class <code>error_condition</code> constructors as indicated:
</p>

<blockquote>
<pre>
<ins>constexpr</ins> error_condition(int val, const error_category<del>&amp;</del><ins>*</ins> cat);
</pre>
<p>
<i>Effects:</i> Constructs an object of type <code>error_condition</code>.
</p>
<p>
<i>Postconditions:</i> <code>val_ == val</code> and <code>cat_ == cat</code>.
</p>
<p>
<i>Throws:</i> Nothing.
</p>
</blockquote>

<p>
Change 19.5.5.3 <a href="https://wg21.link/syserr.errcondition.modifiers">[syserr.errcondition.modifiers]</a> Class <code>error_condition</code> modifiers as indicated:
</p>

<blockquote>
<pre>
void assign(int val, const error_category<del>&amp;</del><ins>*</ins> cat);
</pre>
<p>
<i>Postconditions:</i> <code>val_ == val</code> and <code>cat_ == cat</code>.
</p>
<p>
<i>Throws:</i> Nothing.
</p>
</blockquote>

<p>
Change 19.5.5.4 <a href="https://wg21.link/syserr.errcondition.observers">[syserr.errcondition.observers]</a> Class <code>error_condition</code> observers as indicated:
</p>

<blockquote>
<pre>
const error_category<del>&amp;</del><ins>*</ins> category() const;
</pre>
<p>
<i>Returns:</i> <code>cat_</code>.
</p>
<p>
<i>Throws:</i> Nothing.
</p>
</blockquote>

<p>
Throughout 19.5 <a href="https://wg21.link/syserr">[syserr]</a> System error support, change "<code>category().</code>"  to "<code>category()-&gt;</code>".
Appears approximately six times.
</p>

<p>
<i>[Partially Editorial]</i> In 19.5.6 <a href="https://wg21.link/syserr.compare">[syserr.compare]</a> Comparison operators,
paragraphs 2 and 4, change "<code>category.equivalent(</code>"  to
"<code>category()-&gt;equivalent(</code>".
</p>

<p>
Change 19.5.8.1 <a href="https://wg21.link/syserr.syserr.overview">[syserr.syserr.overview]</a> Class system_error overview as indicated:
</p>

<blockquote><pre>
public:
  system_error(error_code ec, const string&amp; what_arg);
  system_error(error_code ec);
  system_error(int ev, const error_category<del>&amp;</del><ins>*</ins> ecat,
      const string&amp; what_arg);
  system_error(int ev, const error_category<del>&amp;</del><ins>*</ins> ecat);
</pre></blockquote>

<p>
Change 19.5.8.2 <a href="https://wg21.link/syserr.syserr.members">[syserr.syserr.members]</a> Class system_error members as indicated:
</p>

<blockquote>
<pre>
system_error(int ev, const error_category<del>&amp;</del><ins>*</ins> ecat, 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() == error_code(ev, ecat)</code> and
<code>strcmp(runtime_error::what(), what_arg.c_str()) == 0</code>.
</p>
</blockquote>

<pre>
system_error(int ev, const error_category<del>&amp;</del><ins>*</ins> ecat);
</pre>
<blockquote>
<p>
<i>Effects:</i> Constructs an object of class <code>system_error</code>.
</p>
<p>
<i>Postconditions:</i> <code>code() == error_code(ev, ecat)</code> and
<code>strcmp(runtime_error::what(), "") == 0</code>.
</p>
</blockquote>
</blockquote>



<p><b>Rationale:</b></p>
<p><i>[
San Francisco:
]</i></p>


<blockquote><p>
NAD because Beman said so.
</p></blockquote>





</body>
</html>
