<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 2255: [arrays.ts] dynarray constructor ambiguity</title>
<meta property="og:title" content="Issue 2255: [arrays.ts] dynarray constructor ambiguity">
<meta property="og:description" content="C++ library issue. Status: NAD Arrays">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue2255.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_Arrays">NAD Arrays</a> status.</em></p>
<h3 id="2255"><a href="lwg-closed.html#2255">2255</a>. [arrays.ts] <code>dynarray</code> constructor ambiguity</h3>
<p><b>Section:</b> 99 [arrays.ts::dynarray.cons] <b>Status:</b> <a href="lwg-active.html#NAD_Arrays">NAD Arrays</a>
 <b>Submitter:</b> Jonathan Wakely <b>Opened:</b> 2013-04-23 <b>Last modified:</b> 2016-03-08</p>
<p><b>Priority: </b>0
</p>
<p><b>View all issues with</b> <a href="lwg-status.html#NAD Arrays">NAD Arrays</a> status.</p>
<p><b>Discussion:</b></p>
<p><b>Addresses: arrays.ts</b></p>

<p>
These constructors can interact badly::
</p>
<blockquote><pre>
template&lt;class Alloc&gt;
  dynarray(size_type c, const Alloc&amp; alloc);
dynarray(size_type c, const T&amp; v);
</pre></blockquote>
<p>
Unless the second argument is a value of exactly the type <code>T</code> you will get the first constructor, i.e. 
all of these will fail to compile:
</p>
<blockquote><pre>
dynarray&lt;long&gt; dlong(1, 1);   // 1 is not long
dynarray&lt;float&gt; dflt(1, 1.0);  // 1.0 is not float
dynarray&lt;int*&gt; dptr(1, nullptr);  // nullptr is not int*
dynarray&lt;void*&gt; doh(1, 0);  // 0 is not void*
</pre></blockquote>
<p>
The <code>nullptr</code> case is particularly annoying, a user trying to do the right thing by saying <code>nullptr</code> 
instead of <code>NULL</code> still gets the wrong result.
<p/>
The constructor taking an allocator requires that "<code>Alloc</code> shall meet the requirements for an Allocator" 
but doesn't actually say "shall not participate in overload resolution unless ..."
<p/>
I believe we have no precedent for using SFINAE to check "the requirements for an Allocator" because it's 
a pretty complicated set of requirements. We could say it shall not participate in overload resolution if <code>Alloc</code> 
is implicitly convertible to <code>value_type</code>.
<p/>
Alternatively, we could follow the same approach used by other types that can be constructed with an unconstrained 
allocator type and use <code>std::allocator_arg_t</code> as the first argument instead of adding an allocator after the 
other arguments.
</p>

<p><i>[2013-09 Chicago:]</i></p>

<p>
Move to Deferred. This feature will ship after C++14 and should be revisited then.
</p>

<p><i>[2014-06-06 pre-Rapperswil]</i></p>

<p>
This issue has been reopened as arrays-ts.
</p>

<p><i>[2014-06-16 Rapperswil]</i></p>

<p>
Move to Ready for alternative A
</p>

<strong>Previous resolution [SUPERSEDED]:</strong>
<p/>
<blockquote class="note"> 
<ol style="list-style-type:upper-alpha">
<li>
<p>
<em>Either</em> use the correct way to unambiguously call a constructor taking any type of allocator, i.e. change the 
constructors to take <code>dynarray(std::allocator_arg_t, const Alloc&amp;, ...)</code> by modifying both the synopsis
99 [arrays.ts::dynarray.overview] p2 and 99 [arrays.ts::dynarray.cons] before p9 like so:</p>

<blockquote><pre>
template &lt;class Alloc&gt;
  dynarray(<ins>allocator_arg_t, const Alloc&amp; a, </ins>size_type c<del>, const Alloc&amp; alloc</del>);
template &lt;class Alloc&gt;
  dynarray(<ins>allocator_arg_t, const Alloc&amp; a, </ins>size_type c, const T&amp; v<del>, const Alloc&amp; alloc</del>);
template &lt;class Alloc&gt;
  dynarray(<ins>allocator_arg_t, const Alloc&amp; a, </ins>const dynarray&amp; d<del>, const Alloc&amp; alloc</del>);
template &lt;class Alloc&gt;
  dynarray(<ins>allocator_arg_t, const Alloc&amp; a, </ins>initializer_list&lt;T&gt;<del>, const Alloc&amp; alloc</del>);
</pre></blockquote>

</li>

<li><p><em>or</em> constrain the problematic constructor by adding a new paragraph to 99 [arrays.ts::dynarray.cons]:</p>
<blockquote><pre>
template &lt;class Alloc&gt;
  dynarray(size_type c, const Alloc&amp; alloc);
template &lt;class Alloc&gt;
  dynarray(size_type c, const T&amp; v, const Alloc&amp; alloc);
template &lt;class Alloc&gt;
  dynarray(const dynarray&amp; d, const Alloc&amp; alloc);
template &lt;class Alloc&gt;
  dynarray(initializer_list&lt;T&gt;, const Alloc&amp; alloc);
</pre><blockquote>
<p>
-9- <i>Requires</i>: <code>Alloc</code> shall meet the requirements for an Allocator (16.4.4.6 <a href="https://wg21.link/allocator.requirements">[allocator.requirements]</a>).
<p/>
-10- <i>Effects</i>: Equivalent to the preceding constructors except that each element is constructed with uses-allocator
construction (20.2.8.2 <a href="https://wg21.link/allocator.uses.construction">[allocator.uses.construction]</a>).
<p/>
<ins>-?- <i>Remarks</i>: The first constructor shall not participate in overload resolution unless <code>Alloc</code> is not 
implicitly convertible to <code>T</code>.</ins>
</p>
</blockquote></blockquote>
</li>
</ol>
</blockquote>

<p><i>[2014/11 Urbana]</i></p>

<p>
Held at Ready status, pending clarification of Arrays TS
</p>



<p id="res-2255"><b>Proposed resolution:</b></p>

<ol>
<li>
<p>
Use the correct way to unambiguously call a constructor taking any type of allocator, i.e. change the 
constructors to take <code>dynarray(std::allocator_arg_t, const Alloc&amp;, ...)</code> by modifying both the synopsis
99 [arrays.ts::dynarray.overview] p2 and 99 [arrays.ts::dynarray.cons] before p9 like so:</p>

<blockquote><pre>
template &lt;class Alloc&gt;
  dynarray(<ins>allocator_arg_t, const Alloc&amp; a, </ins>size_type c<del>, const Alloc&amp; alloc</del>);
template &lt;class Alloc&gt;
  dynarray(<ins>allocator_arg_t, const Alloc&amp; a, </ins>size_type c, const T&amp; v<del>, const Alloc&amp; alloc</del>);
template &lt;class Alloc&gt;
  dynarray(<ins>allocator_arg_t, const Alloc&amp; a, </ins>const dynarray&amp; d<del>, const Alloc&amp; alloc</del>);
template &lt;class Alloc&gt;
  dynarray(<ins>allocator_arg_t, const Alloc&amp; a, </ins>initializer_list&lt;T&gt;<del>, const Alloc&amp; alloc</del>);
</pre></blockquote>

</li>
</ol>






</body>
</html>
