<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 2510: Tag types should not be DefaultConstructible</title>
<meta property="og:title" content="Issue 2510: Tag types should not be DefaultConstructible">
<meta property="og:description" content="C++ library issue. Status: C++17">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue2510.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++17">C++17</a> status.</em></p>
<h3 id="2510"><a href="lwg-defects.html#2510">2510</a>. Tag types should not be <code>DefaultConstructible</code></h3>
<p><b>Section:</b> 17.6 <a href="https://wg21.link/support.dynamic">[support.dynamic]</a>, 22.2 <a href="https://wg21.link/utility">[utility]</a>, 22.3.5 <a href="https://wg21.link/pair.piecewise">[pair.piecewise]</a>, 20.2.2 <a href="https://wg21.link/memory.syn">[memory.syn]</a>, 20.2.7 <a href="https://wg21.link/allocator.tag">[allocator.tag]</a>, 32.6 <a href="https://wg21.link/thread.mutex">[thread.mutex]</a> <b>Status:</b> <a href="lwg-active.html#C++17">C++17</a>
 <b>Submitter:</b> Ville Voutilainen <b>Opened:</b> 2015-06-13 <b>Last modified:</b> 2017-07-30</p>
<p><b>Priority: </b>2
</p>
<p><b>View all other</b> <a href="lwg-index.html#support.dynamic">issues</a> in [support.dynamic].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#C++17">C++17</a> status.</p>
<p><b>Discussion:</b></p>
<p>
<code>std::experimental::optional</code>, for certain reasons, specifies its <code>nullopt</code> type
to not be <code>DefaultConstructible</code>. It doesn't do so for its tag type <code>in_place_t</code>
and neither does the standard proper for any of its tag types. That turns
out to be very unfortunate, consider the following:
</p>
<blockquote><pre>
#include &lt;memory&gt;
#include &lt;array&gt;

void f(std::array&lt;int, 1&gt;, int) {} // #1
void f(std::allocator_arg_t, int) {} // #2

int main()
{
  f({}, 666); // #3
}
</pre></blockquote>
<p>
The call at #3 is ambiguous. What's even worse is that if the overload #1
is removed, the call works just fine. The whole point of a tag type is that
it either needs to mentioned in a call or it needs to be a forwarded argument,
so being able to construct a tag type like that makes no sense.
<p/>
Making the types have an <em>explicit default constructor</em> might have helped, but 
<a href="http://open-std.org/JTC1/SC22/WG21/docs/cwg_active.html#1518">CWG 1518</a>
is going against that idea.
<p/>
[optional.nullopt]/3 solves this problem for <code>nullopt</code>:
</p>
<blockquote>
<p>
Type <code>nullopt_t</code> shall not have a default constructor. It shall be a
literal type. Constant <code>nullopt</code> shall be initialized with an argument
of literal type.
</p>
</blockquote>

<p><i>[2015-06, Telecon]</i></p>

<p>
Move to Tentatively Ready.
</p>

<p><i>[2015-10, Kona Saturday afternoon]</i></p>

<p>Move back to Open</p>
<p>JW: The linked Core issue (CWG 1518) gives us a better tool to solve this (explicit default constructors). [The CWG Issue means that an explicit default constructor will no longer match "{}".] JW explains that it's important that tag types cannot be constructed from "{}" (e.g. the allocator tag in the tuple constructors).</p>
<p>WEB: Should we now go back and update our constructors? JW: For tag types, yes.</p>
<p>VV: The guideline is that anything that does not mention the type name explicitly should not invoke an explicit constructor.</p>
<p>Ville will provide wording.</p>
<p>Discussion about pair/tuple's default constructor - should they now be explicit?</p>

<p><i>[2016-01-31]</i></p>

<p>
Ville provides revised wording.
</p>

<p>
<strong>Previous resolution [SUPERSEDED]:</strong>
</p>
<blockquote class="note">
<p>
This wording is relative to N4527.
</p>

<ol>
<li><p>In 17.6 <a href="https://wg21.link/support.dynamic">[support.dynamic]</a>/1, change the header <code>&lt;new&gt;</code> synopsis:</p>

<blockquote><pre>
[&hellip;]
struct nothrow_t <del>{}</del>; <ins><i>see below</i></ins>
extern const nothrow_t nothrow;
[&hellip;]
</pre></blockquote>
</li>
<li><p>Add a new paragraph after 17.6 <a href="https://wg21.link/support.dynamic">[support.dynamic]</a>/1 (<em>following</em> the header <code>&lt;new&gt;</code> synopsis):</p>

<blockquote>
<p>
<ins>-?- Type <code>nothrow_t</code> shall not have a default constructor.</ins>
</p>
</blockquote>
</li>
<li><p>In 22.2 <a href="https://wg21.link/utility">[utility]</a>/2, change the header <code>&lt;utility&gt;</code> synopsis:</p>

<blockquote><pre>
[&hellip;]
// <i>20.3.5, pair piecewise construction</i>
struct piecewise_construct_t <del>{ }</del>; <ins><i>see below</i></ins>
constexpr piecewise_construct_t piecewise_construct{ <ins><i>unspecified</i></ins> };
[&hellip;]
</pre></blockquote>
</li>
<li><p>Add a new paragraph after 22.2 <a href="https://wg21.link/utility">[utility]</a>/2 (<em>following</em> the header <code>&lt;utility&gt;</code> synopsis):</p>

<blockquote>
<p>
<ins>-?- Type <code>piecewise_construct_t</code> shall not have a default constructor. It shall
be a literal type. Constant <code>piecewise_construct</code> shall be initialized with an argument of literal type.</ins>
</p>
</blockquote>
</li>
<li><p>In 22.3.5 <a href="https://wg21.link/pair.piecewise">[pair.piecewise]</a>, apply the following edits:</p>

<blockquote><pre>
struct piecewise_construct_t <del>{ }</del>;
constexpr piecewise_construct_t piecewise_construct{ <ins><i>unspecified</i></ins> };
</pre></blockquote>
</li>
<li><p>In 20.2.2 <a href="https://wg21.link/memory.syn">[memory.syn]</a>/1, change the header <code>&lt;memory&gt;</code> synopsis:</p>

<blockquote><pre>
[&hellip;]
// <i>20.7.6, allocator argument tag</i>
struct allocator_arg_t <del>{ }</del>; <ins><i>see below</i></ins>
constexpr allocator_arg_t allocator_arg{ <ins><i>unspecified</i></ins> };
[&hellip;]
</pre></blockquote>
</li>
<li><p>Add a new paragraph after 20.2.2 <a href="https://wg21.link/memory.syn">[memory.syn]</a>/1 (<em>following</em> the header <code>&lt;memory&gt;</code> synopsis):</p>

<blockquote>
<p>
<ins>-?- Type <code>allocator_arg_t</code> shall not have a default constructor. It shall
be a literal type. Constant <code>allocator_arg</code> shall be initialized with an
argument of literal type.</ins>
</p>
</blockquote>
</li>
<li><p>In 20.2.7 <a href="https://wg21.link/allocator.tag">[allocator.tag]</a>, apply the following edits:</p>

<blockquote><pre>
namespace std {
  struct allocator_arg_t <del>{ }</del>;
  constexpr allocator_arg_t allocator_arg{ <ins><i>unspecified</i></ins> };
}
</pre></blockquote>
<blockquote class="note">
<p>
Editorial drive-by: <code>piecewise_construct_t</code> is written, in 22.3.5 <a href="https://wg21.link/pair.piecewise">[pair.piecewise]</a> like
</p>
<blockquote><pre>
struct piecewise_construct_t { };
constexpr piecewise_construct_t piecewise_construct{};
</pre></blockquote>
<p>
whereas other tag types such as <code>allocator_construct_t</code> are, in e.g.
20.2.7 <a href="https://wg21.link/allocator.tag">[allocator.tag]</a>, written like
</p>
<blockquote><pre>
namespace std {
  struct allocator_arg_t { };
  constexpr allocator_arg_t allocator_arg{};
}
</pre></blockquote>
<p>
We should decide whether or not to write out the <code>std</code> namespace in such
paragraphs. I would suggest not to write it out.
</p>
</blockquote>

</li>
<li><p>In 32.6 <a href="https://wg21.link/thread.mutex">[thread.mutex]</a>/1, change the header <code>&lt;mutex&gt;</code> synopsis:</p>

<blockquote><pre>
[&hellip;]
struct defer_lock_t <del>{ }</del>; <ins><i>see below</i></ins>
struct try_to_lock_t <del>{ }</del>; <ins><i>see below</i></ins>
struct adopt_lock_t <del>{ }</del>; <ins><i>see below</i></ins>

constexpr defer_lock_t defer_lock { <ins><i>unspecified </i></ins> };
constexpr try_to_lock_t try_to_lock { <ins><i>unspecified </i></ins> };
constexpr adopt_lock_t adopt_lock { <ins><i>unspecified </i></ins> };
[&hellip;]
</pre></blockquote>
</li>
<li><p>Add three new paragraphs after [thread.mutex]/1 (<em>following</em> the header <code>&lt;mutex&gt;</code> synopsis):</p>

<blockquote>
<p>
<ins>-?- Type <code>defer_lock_t</code> shall not have a default constructor. It shall
be a literal type. Constant <code>defer_lock</code> shall be initialized with an
argument of literal type.</ins>
<p/>
<ins>-?- Type <code>try_to_lock_t</code> shall not have a default constructor. It shall
be a literal type. Constant <code>try_to_lock</code> shall be initialized with an
argument of literal type.</ins>
<p/>
<ins>-?- Type <code>adopt_lock_t</code> shall not have a default constructor. It shall
be a literal type. Constant <code>adopt_lock</code> shall be initialized with an
argument of literal type.</ins>
</p>
</blockquote>

</li>
</ol>
</blockquote>

<p><i>[2016-03 Jacksonville]</i></p>

<p>
AM: should have note about compatibility in Annex C<br/>
HH: like this idiom well enough that I've started using it in my own code<br/>
AM: why are pair and tuple involved here?<br/>
GR: they are the only types which forward explicitness with EXPLICIT<br/>
AM: British spelling of behaviour<br/>
AM: happy to drop my issue about Annex C<br/>
</p>

<p><i>[2016-06 Oulu]</i></p>

<p>This is waiting on Core issue 1518</p>
<p>Saturday: Core 1518 was resolved in Oulu</p>

<p><i>[2016-07 Chicago]</i></p>

<p>This is related to <a href="lwg-defects.html#2736" title="nullopt_t insufficiently constrained (Status: C++17)">2736</a><sup><a href="https://cplusplus.github.io/LWG/issue2736" title="Latest snapshot">(i)</a></sup></p>
<p>Monday PM: Moved to Tentatively Ready</p>


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

<ol>
<li><p>In 17.6 <a href="https://wg21.link/support.dynamic">[support.dynamic]</a>/1, change the header <code>&lt;new&gt;</code> synopsis:</p>

<blockquote><pre>
[&hellip;]
struct nothrow_t { <ins>explicit nothrow_t() = default;</ins> };
extern const nothrow_t nothrow;
[&hellip;]
</pre></blockquote>
</li>

<li><p>In 22.2 <a href="https://wg21.link/utility">[utility]</a>/2, change the header <code>&lt;utility&gt;</code> synopsis:</p>

<blockquote><pre>
[&hellip;]
// <i>20.3.5, pair piecewise construction</i>
struct piecewise_construct_t { <ins>explicit piecewise_construct_t() = default;</ins> };
constexpr piecewise_construct_t piecewise_construct{};
[&hellip;]
</pre></blockquote>
</li>

<li><p>In 22.3.2 <a href="https://wg21.link/pairs.pair">[pairs.pair]</a>, change the class template <code>pair</code> synopsis:</p>

<blockquote><pre>
[&hellip;]
pair(pair&amp;&amp;) = default;
<ins><i>EXPLICIT</i></ins> constexpr pair();
<i>EXPLICIT</i> constexpr pair(const T1&amp; x, const T2&amp; y);
[&hellip;]
</pre></blockquote>
</li>

<li><p>Around 22.3.2 <a href="https://wg21.link/pairs.pair">[pairs.pair]</a> p3, apply the following edits:</p>

<blockquote>
<pre>
<ins><i>EXPLICIT</i></ins> constexpr pair();
</pre>
<blockquote>
<p>
-3- <i>Effects</i>: Value-initializes <code>first</code> and <code>second</code>.
<p/>
-4- <i>Remarks</i>: This constructor shall not participate in overload resolution unless 
<code>is_default_constructible&lt;first_type&gt;::value</code> is <code>true</code> and 
<code>is_default_constructible&lt;second_type&gt;::value</code> is <code>true</code>. 
[<i>Note</i>: This
behaviour can be implemented by a constructor template with default template arguments. &mdash; 
<i>end note</i>]
<ins>The constructor is explicit if and only if either <code>first_type</code> or <code>second_type</code> 
is not implicitly default-constructible. 
[<i>Note</i>: This behaviour can be implemented with a trait 
that checks whether a <code>const first_type&amp;</code> or a <code>const second_type&amp;</code> can be 
initialized with <code>{}</code>. &mdash; <i>end note</i>]</ins>
</p>
</blockquote>
</blockquote>
</li>

<li><p>In 22.3.5 <a href="https://wg21.link/pair.piecewise">[pair.piecewise]</a>, apply the following edits:</p>

<blockquote><pre>
struct piecewise_construct_t { <ins>explicit piecewise_construct_t() = default;</ins> };
constexpr piecewise_construct_t piecewise_construct{};
</pre></blockquote>
</li>

<li><p>In 22.4.4 <a href="https://wg21.link/tuple.tuple">[tuple.tuple]</a>, change the class template <code>tuple</code> synopsis:</p>

<blockquote><pre>
[&hellip;]
<i>// 20.4.2.1, tuple construction</i>
<ins><i>EXPLICIT</i></ins> constexpr tuple();
<i>EXPLICIT</i> constexpr tuple(const Types&amp;...); <i>// only if sizeof...(Types) &gt;= 1</i>
[&hellip;]
</pre></blockquote>
</li>

<li><p>Around 22.4.4.2 <a href="https://wg21.link/tuple.cnstr">[tuple.cnstr]</a> p4, apply the following edits:</p>

<blockquote>
<pre>
<ins><i>EXPLICIT</i></ins> constexpr tuple();
</pre>
<blockquote>
<p>
-4- <i>Effects</i>: Value initializes each element.
<p/>
-5- <i>Remarks</i>: This constructor shall not participate in overload resolution unless 
<code>is_default_constructible&lt;<i>T<sub>i</sub></i>&gt;::value</code> is <code>true</code> for all <i>i</i>. 
[<i>Note</i>: This
behaviour can be implemented by a constructor template with default template arguments. &mdash; 
<i>end note</i>]
<ins>The constructor is explicit if and only if <code><i>T<sub>i</sub></i></code> 
is not implicitly default-constructible for at least one <i>i</i>.
[<i>Note</i>: This behaviour can be implemented with a trait that checks whether
a <code>const Ti&amp;</code> can be initialized with <code>{}</code>. &mdash; <i>end note</i>]</ins>
</p>
</blockquote>
</blockquote>
</li>

<li><p>In 20.2.2 <a href="https://wg21.link/memory.syn">[memory.syn]</a>/1, change the header <code>&lt;memory&gt;</code> synopsis:</p>

<blockquote><pre>
[&hellip;]
// <i>20.7.6, allocator argument tag</i>
struct allocator_arg_t { <ins>explicit allocator_arg_t() = default;</ins> };
constexpr allocator_arg_t allocator_arg{};
[&hellip;]
</pre></blockquote>
</li>

<li><p>In 20.2.7 <a href="https://wg21.link/allocator.tag">[allocator.tag]</a>, apply the following edits:</p>

<blockquote><pre>
namespace std {
  struct allocator_arg_t { <ins>explicit allocator_arg_t() = default;</ins> };
  constexpr allocator_arg_t allocator_arg{};
}
</pre></blockquote>
<blockquote class="note">
<p>
Editorial drive-by: <code>piecewise_construct_t</code> is written, in 22.3.5 <a href="https://wg21.link/pair.piecewise">[pair.piecewise]</a> like
</p>
<blockquote><pre>
struct piecewise_construct_t { };
constexpr piecewise_construct_t piecewise_construct{};
</pre></blockquote>
<p>
whereas other tag types such as <code>allocator_construct_t</code> are, in e.g.
20.2.7 <a href="https://wg21.link/allocator.tag">[allocator.tag]</a>, written like
</p>
<blockquote><pre>
namespace std {
  struct allocator_arg_t { };
  constexpr allocator_arg_t allocator_arg{};
}
</pre></blockquote>
<p>
We should decide whether or not to write out the <code>std</code> namespace in such
paragraphs. I would suggest not to write it out.
</p>
</blockquote>
</li>

<li><p>In 32.6 <a href="https://wg21.link/thread.mutex">[thread.mutex]</a>/1, change the header <code>&lt;mutex&gt;</code> synopsis:</p>

<blockquote><pre>
[&hellip;]
struct defer_lock_t { <ins>explicit defer_lock_t() = default;</ins> };
struct try_to_lock_t { <ins>explicit try_to_lock_t() = default;</ins> };
struct adopt_lock_t { <ins>explicit adopt_lock_t() = default;</ins> };

constexpr defer_lock_t defer_lock { };
constexpr try_to_lock_t try_to_lock { };
constexpr adopt_lock_t adopt_lock { };
[&hellip;]
</pre></blockquote>
</li>

</ol>





</body>
</html>
