<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 2901: Variants cannot properly support allocators</title>
<meta property="og:title" content="Issue 2901: Variants cannot properly support allocators">
<meta property="og:description" content="C++ library issue. Status: C++17">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue2901.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="2901"><a href="lwg-defects.html#2901">2901</a>. Variants cannot properly support allocators</h3>
<p><b>Section:</b> 22.6.3 <a href="https://wg21.link/variant.variant">[variant.variant]</a> <b>Status:</b> <a href="lwg-active.html#C++17">C++17</a>
 <b>Submitter:</b> United States <b>Opened:</b> 2017-02-03 <b>Last modified:</b> 2020-09-06</p>
<p><b>Priority: </b>0
</p>
<p><b>View all other</b> <a href="lwg-index.html#variant.variant">issues</a> in [variant.variant].</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>
<b>Addresses US 113</b>

<p>Variants cannot properly support allocators, as any assignment of a subsequent value throws away the allocator used at 
construction. This is not an easy problem to solve, so <code>variant</code> would be better served dropping the illusion of 
allocator support for now, leaving open the possibility to provide proper support once the problems are fully understood.</p>

<p>Proposed change: Strike the 8 allocator aware constructor overloads from the class definition, and strike 
20.7.2.1 [variant.ctor] p34/35. Strike clause 20.7.12 [variant.traits]. Strike the specialization of 
<code>uses_allocator</code> for <code>variant</code> in the <code>&lt;variant&gt;</code> header synopsis, 20.7.1 [variant.general].</p>

<p><i>[2017-02-28, Kona, Casey provides wording]</i></p>


<p><i>[2017-06-29 Moved to Tentatively Ready after 7 positive votes on c++std-lib.]</i></p>


<p><i>[2017-07 Toronto Moved to Immediate]</i></p>



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

<ol>
<li><p>Change 22.6.2 <a href="https://wg21.link/variant.syn">[variant.syn]</a>, header <code>&lt;variant&gt;</code>  synopsis, as follows:</p>

<blockquote><pre>
[&hellip;]
<del><i>//  [variant.traits], allocator-related traits</i>
template &lt;class T, class Alloc&gt; struct uses_allocator;
template &lt;class... Types, class Alloc&gt; struct uses_allocator&lt;variant&lt;Types...&gt;, Alloc&gt;;</del>
</pre></blockquote>
</li>

<li><p>Change 22.6.3 <a href="https://wg21.link/variant.variant">[variant.variant]</a>, class template <code>variant</code>  synopsis, as follows:</p>

<blockquote><pre>
[&hellip;]
<del><i>// allocator-extended constructors</i>
template &lt;class Alloc&gt;
  variant(allocator_arg_t, const Alloc&amp;);
template &lt;class Alloc&gt;
  variant(allocator_arg_t, const Alloc&amp;, const variant&amp;);
template &lt;class Alloc&gt;
  variant(allocator_arg_t, const Alloc&amp;, variant&amp;&amp;);
template &lt;class Alloc, class T&gt;
  variant(allocator_arg_t, const Alloc&amp;, T&amp;&amp;);
template &lt;class Alloc, class T, class... Args&gt;
  variant(allocator_arg_t, const Alloc&amp;, in_place_type_t&lt;T&gt;, Args&amp;&amp;...);
template &lt;class Alloc, class T, class U, class... Args&gt;
  variant(allocator_arg_t, const Alloc&amp;, in_place_type_t&lt;T&gt;,
          initializer_list&lt;U&gt;, Args&amp;&amp;...);
template &lt;class Alloc, size_t I, class... Args&gt;
  variant(allocator_arg_t, const Alloc&amp;, in_place_index_t&lt;I&gt;, Args&amp;&amp;...);
template &lt;class Alloc, size_t I, class U, class... Args&gt;
  variant(allocator_arg_t, const Alloc&amp;, in_place_index_t&lt;I&gt;,
          initializer_list&lt;U&gt;, Args&amp;&amp;...);</del>
</pre></blockquote>
</li>

<li><p>Modify 22.6.3.2 <a href="https://wg21.link/variant.ctor">[variant.ctor]</a> as indicated:</p>
<blockquote><pre>
<del><i>// allocator-extended constructors</i>
template &lt;class Alloc&gt;
  variant(allocator_arg_t, const Alloc&amp; a);
template &lt;class Alloc&gt;
  variant(allocator_arg_t, const Alloc&amp; a, const variant&amp; v);
template &lt;class Alloc&gt;
  variant(allocator_arg_t, const Alloc&amp; a, variant&amp;&amp; v);
template &lt;class Alloc, class T&gt;
  variant(allocator_arg_t, const Alloc&amp; a, T&amp;&amp; t);
template &lt;class Alloc, class T, class... Args&gt;
  variant(allocator_arg_t, const Alloc&amp; a, in_place_type_t&lt;T&gt;, Args&amp;&amp;... args);
template &lt;class Alloc, class T, class U, class... Args&gt;
  variant(allocator_arg_t, const Alloc&amp; a, in_place_type_t&lt;T&gt;,
          initializer_list&lt;U&gt; il, Args&amp;&amp;... args);
template &lt;class Alloc, size_t I, class... Args&gt;
  variant(allocator_arg_t, const Alloc&amp; a, in_place_index_t&lt;I&gt;, Args&amp;&amp;... args);
template &lt;class Alloc, size_t I, class U, class... Args&gt;
  variant(allocator_arg_t, const Alloc&amp; a, in_place_index_t&lt;I&gt;,
          initializer_list&lt;U&gt; il, Args&amp;&amp;... args);</del>
</pre>
<blockquote>
<p>
<del>-34- <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>).</del>
<p/>
<del>-35- <i>Effects:</i> Equivalent to the preceding constructors except that the contained value is constructed with
uses-allocator construction (20.2.8.2 <a href="https://wg21.link/allocator.uses.construction">[allocator.uses.construction]</a>).</del>
</p>
</blockquote>
</blockquote>
</li>

<li><p>Modify  [variant.traits] as indicated:</p>

<blockquote>
<pre>
<del>template &lt;class... Types, class Alloc&gt;
struct uses_allocator&lt;variant&lt;Types...&gt;, Alloc&gt; : true_type { };</del>
</pre>
<blockquote>
<p>
<del>-1- <i>Requires:</i> <code>Alloc</code> shall be an Allocator (16.4.4.6 <a href="https://wg21.link/allocator.requirements">[allocator.requirements]</a>).</del>
<p/>
<del>-2- [<i>Note:</i> Specialization of this trait informs other library components that variant can be constructed
with an allocator, even though it does not have a nested <code>allocator_type</code>. &mdash; <i>end note</i>]</del>
</p>
</blockquote>
</blockquote>
</li>
</ol>





</body>
</html>
