<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 2992: system_category() and error_code::error_code() should be constexpr</title>
<meta property="og:title" content="Issue 2992: system_category() and error_code::error_code() should be constexpr">
<meta property="og:description" content="C++ library issue. Status: NAD">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue2992.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="2992"><a href="lwg-closed.html#2992">2992</a>. <code>system_category()</code> and <code>error_code::error_code()</code> should be <code>constexpr</code></h3>
<p><b>Section:</b> 19.5.3.5 <a href="https://wg21.link/syserr.errcat.objects">[syserr.errcat.objects]</a>, 19.5.4.2 <a href="https://wg21.link/syserr.errcode.constructors">[syserr.errcode.constructors]</a> <b>Status:</b> <a href="lwg-active.html#NAD">NAD</a>
 <b>Submitter:</b> Peter Dimov <b>Opened:</b> 2017-06-27 <b>Last modified:</b> 2017-07-12</p>
<p><b>Priority: </b>Not Prioritized
</p>
<p><b>View all other</b> <a href="lwg-index.html#syserr.errcat.objects">issues</a> in [syserr.errcat.objects].</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 default constructor of <code>error_code</code> should be <code>constexpr</code> to enable constant initialization; 
as a practical matter, there are reports that it regularly shows up in profiles because clearing error codes 
is so frequent.
<p/>
Suggested resolution:
</p>
<ul>
<li><p>add <code>constexpr</code> to the declaration of <code>system_category()</code> in [syserr.errcat.overview] 
and [syserr.errcat.objects];</p></li>
<li><p>optionally, add <code>constexpr</code> to the declaration of <code>generic_category()</code> in the same two sections;</p></li>
<li><p>add <code>constexpr</code> to the default constructor of <code>error_code</code> in [syserr.errcode.overview] and [syserr.errcode.constructors];</p></li>
<li><p>optionally, add <code>constexpr</code> to the <code>error_code(int val, const error_category&amp; cat)</code> 
constructor in the same two sections;</p></li>
<li><p>optionally, add <code>constexpr</code> to <code>error_code::assign</code>;</p></li>
<li><p>optionally, add <code>constexpr</code> to <code>error_code::clear</code>;</p></li>
<li><p>optionally, add <code>constexpr</code> to <code>error_code::value</code>;</p></li>
<li><p>optionally, add <code>constexpr</code> to <code>error_code::category</code>.</p></li>
</ul>
<p>
There was an objection that <code>system_category()</code> can't be made <code>constexpr</code> because it needs to 
"immortalize" the object so that it's not destroyed at process shutdown or module unload, in order for 
the <code>error_code</code> facility to remain usable. However, the <a href="https://wandbox.org/permlink/pwliJ6sw6I4LBtlE">following 
proof of concept</a> shows how to achieve this and still make the function <code>constexpr</code>:
</p>
<blockquote><pre>
#include &lt;new&gt;

template&lt;class _Ty&gt;
  union _Immortalizer
  { // constructs _Ty, never destroys
  constexpr _Immortalizer(): __ty()
  {
  }

  ~_Immortalizer() noexcept {}
  _Immortalizer(const _Immortalizer&amp;) = delete;
  _Immortalizer&amp; operator=(const _Immortalizer&amp;) = delete;

  _Ty __ty;
};

struct error_category
{
  virtual ~error_category() = default;
};

struct system_category_impl : public error_category
{
};

[[clang::require_constant_initialization]] static const _Immortalizer&lt;system_category_impl&gt; _System_category;

constexpr error_category const&amp; system_category() noexcept
{
  return _System_category.__ty;
}

struct error_code
{
  int val_;
  const error_category* cat_;

  constexpr error_code() noexcept : val_(0), cat_(&amp;system_category()) {}

  constexpr int value() const noexcept { return val_; }
  constexpr error_category const&amp; category() const noexcept { return *cat_; }
};

constexpr error_code s_code;

static_assert(s_code.value() == 0);
static_assert(&amp;s_code.category() == &amp;system_category());
</pre></blockquote>

<p><i>[2017-07 Toronto Tuesday PM issue prioritization]</i></p>

<p>NAD; This is a feature request; needs a paper.</p>


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

<ol>
<li><p>Edit 19.5.3.1 <a href="https://wg21.link/syserr.errcat.overview">[syserr.errcat.overview]</a>, class <code>error_category</code> synopsis, as indicated:</p>

<blockquote>
<pre>
class error_category {
public:
  constexpr error_category() noexcept;
  virtual ~error_category();
  error_category(const error_category&amp;) = delete;
  error_category&amp; operator=(const error_category&amp;) = delete;
  virtual const char* name() const noexcept = 0;
  virtual error_condition default_error_condition(int ev) const noexcept;
  virtual bool equivalent(int code, const error_condition&amp; condition) const noexcept;
  virtual bool equivalent(const error_code&amp; code, int condition) const noexcept;
  virtual string message(int ev) const = 0;

  bool operator==(const error_category&amp; rhs) const noexcept;
  bool operator!=(const error_category&amp; rhs) const noexcept;
  bool operator&lt;(const error_category&amp; rhs) const noexcept;
};

<ins>constexpr</ins> const error_category&amp; generic_category() noexcept;
<ins>constexpr</ins> const error_category&amp; system_category() noexcept;
</pre>
</blockquote>
</li>

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

<blockquote>
<pre>
<ins>constexpr</ins> const error_category&amp; generic_category() noexcept;
</pre>
<blockquote>
<p>
[&hellip;]
</p>
</blockquote>
<pre>
<ins>constexpr</ins> const error_category&amp; system_category() noexcept;
</pre>
<blockquote>
<p>
[&hellip;]
</p>
</blockquote>
</blockquote>
</li>

<li><p>Edit 19.5.4.1 <a href="https://wg21.link/syserr.errcode.overview">[syserr.errcode.overview]</a>, class <code>error_code</code> synopsis, as indicated:</p>

<blockquote>
<pre>
class error_code {
public:
  // 19.5.4.2 <a href="https://wg21.link/syserr.errcode.constructors">[syserr.errcode.constructors]</a>, constructors
  <ins>constexpr</ins> error_code() noexcept;
  <ins>constexpr</ins> error_code(int val, const error_category&amp; cat) noexcept;
  template &lt;class ErrorCodeEnum&gt;
  error_code(ErrorCodeEnum e) noexcept;
  
  // 19.5.4.3 <a href="https://wg21.link/syserr.errcode.modifiers">[syserr.errcode.modifiers]</a>, modifiers
  <ins>constexpr</ins> void assign(int val, const error_category&amp; cat) noexcept;
  template &lt;class ErrorCodeEnum&gt;
  error_code&amp; operator=(ErrorCodeEnum e) noexcept;
  <ins>constexpr</ins> void clear() noexcept;
  
  // 19.5.4.4 <a href="https://wg21.link/syserr.errcode.observers">[syserr.errcode.observers]</a>, observers
  <ins>constexpr</ins> int value() const noexcept;
  <ins>constexpr</ins> const error_category&amp; category() const noexcept;
  error_condition default_error_condition() const noexcept;
  string message() const;
  explicit operator bool() const noexcept;
  
private:
  int val_; // exposition only
  const error_category* cat_; // exposition only
};</pre>
</blockquote>
</li>

<li><p>Edit 19.5.4.2 <a href="https://wg21.link/syserr.errcode.constructors">[syserr.errcode.constructors]</a> as indicated:</p>

<blockquote>
<pre>
<ins>constexpr</ins> error_code() noexcept;
</pre>
<blockquote>
<p>
[&hellip;]
</p>
</blockquote>
<pre>
<ins>constexpr</ins> error_code(int val, const error_category&amp; cat) noexcept;
</pre>
<blockquote>
<p>
[&hellip;]
</p>
</blockquote>
</blockquote>
</li>

<li><p>Edit 19.5.4.3 <a href="https://wg21.link/syserr.errcode.modifiers">[syserr.errcode.modifiers]</a> as indicated:</p>

<blockquote>
<pre>
<ins>constexpr</ins> void assign(int val, const error_category&amp; cat) noexcept;
</pre>
<blockquote>
<p>
[&hellip;]
</p>
</blockquote>
<pre>
<ins>constexpr</ins> void clear() noexcept;
</pre>
<blockquote>
<p>
[&hellip;]
</p>
</blockquote>
</blockquote>
</li>

<li><p>Edit 19.5.4.4 <a href="https://wg21.link/syserr.errcode.observers">[syserr.errcode.observers]</a> as indicated:</p>

<blockquote>
<pre>
<ins>constexpr</ins> int value() const noexcept;
</pre>
<blockquote>
<p>
[&hellip;]
</p>
</blockquote>
<pre>
<ins>constexpr</ins> const error_category&amp; category() const noexcept;
</pre>
<blockquote>
<p>
[&hellip;]
</p>
</blockquote>
</blockquote>
</li>
</ol>




</body>
</html>
