<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 2145: error_category default constructor</title>
<meta property="og:title" content="Issue 2145: error_category default constructor">
<meta property="og:description" content="C++ library issue. Status: C++14">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue2145.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++14">C++14</a> status.</em></p>
<h3 id="2145"><a href="lwg-defects.html#2145">2145</a>. <code>error_category</code> default constructor</h3>
<p><b>Section:</b> 19.5.3 <a href="https://wg21.link/syserr.errcat">[syserr.errcat]</a> <b>Status:</b> <a href="lwg-active.html#C++14">C++14</a>
 <b>Submitter:</b> Howard Hinnant <b>Opened:</b> 2012-03-21 <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.errcat">issues</a> in [syserr.errcat].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#C++14">C++14</a> status.</p>
<p><b>Discussion:</b></p>

<p>
Should <code>error_category</code> have a default constructor?
<p/>
If you look at the synopsis in 19.5.3.1 <a href="https://wg21.link/syserr.errcat.overview">[syserr.errcat.overview]</a>, it appears the answer is no. There 
is no default constructor declared and there is another constructor declared (which should inhibit a default 
constructor).
<p/>
However in paragraph 1 of the same section, descriptive text says:
</p>
<blockquote><p>
Classes may be derived from <code>error_category</code> to support categories of errors in addition to those 
defined in this International Standard.
</p></blockquote>
<p>
How shall classes derived from <code>error_category</code> construct their base?
<p/>
Jonathan Wakely: In <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2006/n2066.html">N2066</a> 
<code>error_category</code> was default-constructible. That is still the case in 
<a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2241.html">N2241</a>, because no other 
constructor is declared. Then later <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2422.htm">N2422</a> 
(<a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2422.htm#Issue6">issue 6</a>) declares 
the copy constructor as deleted, but doesn't add a default constructor, causing it to be no longer
default-constructible. That looks like an oversight to me, and I think there should be a public default 
constructor.
<p/>
Daniel: A default-constructor indeed should be provided to allow user-derived classes as described by the
standard. I suggest this one to be both <code>noexcept</code> and <code>constexpr</code>. The latter allows
user-derived non-abstract classes to take advantage of the special <em>constant initialization</em> rule
of  [basic.start.init] p2 b2 for objects with static (or thread) storage duration in namespace 
scope. Note that a <code>constexpr</code> constructor is feasible here, even though there exists a non-trivial
destructor and even though <code>error_category</code> is not a literal type (see <code>std::mutex</code> for a similar
design choice).
<p/>
In addition to that the proposed resolution fixes another minor glitch: According to 16.3.3.5 <a href="https://wg21.link/functions.within.classes">[functions.within.classes]</a>
virtual destructors require a semantics description. 
<p/>
Alberto Ganesh Barbati: I would suggest to remove <code>=default</code> from the constructor instead. 
Please consider that defaulting a constructor or destructor may actually define them as deleted under certain 
conditions (see 11.4.5 <a href="https://wg21.link/class.ctor">[class.ctor]</a>&#47;5 and 11.4.7 <a href="https://wg21.link/class.dtor">[class.dtor]</a>&#47;5). Removing <code>=default</code> 
is easier than providing wording to ensures that such conditions do not occur.
</p>

<p><i>[2012-10 Portland: move to Ready]</i></p>

<p>
The issue is real and the resolution looks good.
</p>

<p>
Are there similar issues elsewhere in this clause?
</p>

<p>
Potential to add <code>constexpr</code> to more constructors, but clearly a separable issue.
</p>

<p><i>[2013-04-20 Bristol]</i></p>



<p id="res-2145"><b>Proposed resolution:</b></p>
<p>This wording is relative to N3376.</p>

<ol>
<li><p>Modify the class <code>error_category</code> synopsis, 19.5.3.1 <a href="https://wg21.link/syserr.errcat.overview">[syserr.errcat.overview]</a> as indicated:
<em>[Drafting note: According to the general 
<a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2011/n3279.pdf"><code>noexcept</code> library guidelines</a> 
destructors should not have any explicit exception specification. This destructor was overlooked during the paper
analysis &mdash; end note]</em>
</p>

<blockquote><pre>
namespace std {
  class error_category {
  public:
    <ins>constexpr error_category() noexcept;</ins>
    virtual ~error_category() <del>noexcept</del>;
    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;
  };
}
</pre></blockquote>
</li>

<li><p>Before 19.5.3.2 <a href="https://wg21.link/syserr.errcat.virtuals">[syserr.errcat.virtuals]</a> p1 insert a new prototype description as indicated:</p>

<blockquote><pre>
<ins>virtual ~error_category();</ins>
</pre><blockquote>
<p>
<ins>-?- <i>Effects</i>: Destroys an object of class <code>error_category</code>.</ins>
</p>
</blockquote>
</blockquote>

</li>

<li><p>Before 19.5.3.3 <a href="https://wg21.link/syserr.errcat.nonvirtuals">[syserr.errcat.nonvirtuals]</a> p1 insert a new prototype description as indicated:</p>

<blockquote><pre>
<ins>constexpr error_category() noexcept;</ins>
</pre><blockquote>
<p>
<ins>-?- <i>Effects</i>: Constructs an object of class <code>error_category</code>.</ins>
</p>
</blockquote>
</blockquote>

</li>
</ol>






</body>
</html>
