<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 2311: Allocator requirements should be further minimized</title>
<meta property="og:title" content="Issue 2311: Allocator requirements should be further minimized">
<meta property="og:description" content="C++ library issue. Status: NAD">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue2311.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="2311"><a href="lwg-closed.html#2311">2311</a>. Allocator requirements should be further minimized</h3>
<p><b>Section:</b> 16.4.4.6 <a href="https://wg21.link/allocator.requirements">[allocator.requirements]</a> <b>Status:</b> <a href="lwg-active.html#NAD">NAD</a>
 <b>Submitter:</b> Stephan T. Lavavej <b>Opened:</b> 2013-09-21 <b>Last modified:</b> 2016-01-28</p>
<p><b>Priority: </b>2
</p>
<p><b>View other</b> <a href="lwg-index-open.html#allocator.requirements">active issues</a> in [allocator.requirements].</p>
<p><b>View all other</b> <a href="lwg-index.html#allocator.requirements">issues</a> in [allocator.requirements].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#NAD">NAD</a> status.</p>
<p><b>Discussion:</b></p>
<p>
C++11's minimized allocator requirements are great, but they're still requiring more things from users than absolutely necessary.
</p>
<ul>
<li><p>
They require <code>X::value_type</code>, but that can be deduced from <code>SomeAllocator&lt;T, Args&gt;</code>.
</p></li>
<li><p>
They require <code>a1 == a2</code>, but that could default to <code>true</code> as most allocators are stateless.
</p></li>
<li><p>
They require <code>a1 != a2</code>, but if we start requiring STL implementations to go through <code>allocator_traits</code> 
to provide an <code>op==</code> default, we won't need to require <code>op!=</code> from users at all. (<code>std::allocator</code>, 
of course, would continue to provide <code>op!=</code>. Note that this is analogous to <code>reference</code>/<code>const_reference</code> &mdash; 
<code>std::allocator</code> still provides them, but we don't require them from users, and in fact we don't require them to be 
consistent or meaningful if present.)
</p></li>
<li><p>
They require <code>a == b</code> and <code>a != b</code>. This requirement was not present in C++98/03, it is not necessary 
(<code>a == b</code> is always required to be equivalent to rebind-then-compare), and STL implementations don't even need 
to compare allocators of different types directly.
</p></li>
</ul>

<p><i>[2014-02-14 Issaquah: Close as NAD]</i></p>

<p>
Different vendors rely on each of the different elements suggested to be removed.
</p>
<p>
While <code>value_type</code> my be deduced as suggested, far too much wording relies on it being available,
and the standard churn is likely to be much harder than presented here.
</p>



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

<ol>
<li><p>Change in 16.4.4.6 <a href="https://wg21.link/allocator.requirements">[allocator.requirements]</a>, Table 28 &mdash; "Allocator requirements" as indicated:</p>

<blockquote>
<table border="1">
<caption>Table 28 &mdash; Allocator requirements (continued)</caption>
<tr>
<th>Expression</th>
<th>Return type</th>
<th>Assertion&#47;note pre-&#47;post-condition</th>
<th>Default</th>
</tr>

<tr>
<td colspan="4" align="center">
<code>&hellip;</code>
</td>
</tr>

<tr>
<td>
<code>X::value_type</code>
</td>
<td>
Identical to <code>T</code>
</td>
<td>
&nbsp;
</td>
<td>
<ins>See Note B, below.</ins>
</td>
</tr>

<tr>
<td colspan="4" align="center">
<code>&hellip;</code>
</td>
</tr>

<tr>
<td>
<code>a1 == a2</code>
</td>
<td>
<code>bool</code>
</td>
<td>
returns <code>true</code> only if storage<br/>
allocated from each can be<br/>
deallocated via the other.<br/>
<code>operator==</code> shall be reflexive,<br/>
symmetric, and transitive, and<br/>
shall not exit via an exception.<br/>
</td>
<td>
<ins><code>true</code></ins>
</td>
</tr>

<tr>
<td>
<del><code>a1 != a2</code></del>
</td>
<td>
<del><code>bool</code></del>
</td>
<td>
<del>same as <code>!(a1 == a2)</code></del>
</td>
<td>
&nbsp;
</td>
</tr>

<tr>
<td>
<del><code>a == b</code></del>
</td>
<td>
<del><code>bool</code></del>
</td>
<td>
<del>same as <code>a ==<br/>
Y::rebind&lt;T&gt;::other(b)</code></del>
</td>
<td>
&nbsp;
</td>
</tr>

<tr>
<td>
<del><code>a != b</code></del>
</td>
<td>
<del><code>bool</code></del>
</td>
<td>
<del>same as <code>!(a == b)</code></del>
</td>
<td>
&nbsp;
</td>
</tr>

<tr>
<td colspan="4" align="center">
<code>&hellip;</code>
</td>
</tr>

<tr>
<td>
<code>X a(b);</code>
</td>
<td>
&nbsp;
</td>
<td>
Shall not exit via an exception.<br/>
post: <code>Y(a) == b</code>, <code>a == X(b)</code>
</td>
<td>
&nbsp;
</td>
</tr>

<tr>
<td colspan="4" align="center">
<code>&hellip;</code>
</td>
</tr>

<tr>
<td>
<code>X a(move(b));</code>
</td>
<td>
&nbsp;
</td>
<td>
Shall not exit via an exception.<br/>
post: <code>a</code> equals the prior value of <code>X(b)</code>.
</td>
<td>
&nbsp;
</td>
</tr>

<tr>
<td colspan="4" align="center">
<code>&hellip;</code>
</td>
</tr>

</table>
</blockquote>

</li>

<li><p>After 16.4.4.6 <a href="https://wg21.link/allocator.requirements">[allocator.requirements]</a> p3, add a new paragraph:</p>

<blockquote>
<p>
<ins>Note B: If <code>Allocator</code> is a class template instantiation of the form <code>SomeAllocator&lt;T, Args&gt;</code>, 
where <code>Args</code> is zero or more type arguments, and <code>Allocator</code> does not supply a nested type named <code>value_type</code>, 
the standard <code>allocator_traits</code> template uses <code>T</code> in place of <code>Allocator::value_type</code> by default. 
For allocator types that are not template instantiations of the above form, no default is provided.</ins>
</p>
</blockquote>
</li>

<li><p>In the example provided in 16.4.4.6 <a href="https://wg21.link/allocator.requirements">[allocator.requirements]</a>/5, delete as indicated:</p>

<blockquote>
<pre>
template &lt;class Tp&gt;
struct SimpleAllocator {
  <del>typedef Tp value_type;</del>
  SimpleAllocator(ctor args);
  template &lt;class T&gt; SimpleAllocator(const SimpleAllocator&lt;T&gt;&amp; other);
  Tp *allocate(std::size_t n);
  void deallocate(Tp *p, std::size_t n);
};

<del>template &lt;class T, class U&gt;
bool operator==(const SimpleAllocator&lt;T&gt;&amp;, const SimpleAllocator&lt;U&gt;&amp;);
template &lt;class T, class U&gt;
bool operator!=(const SimpleAllocator&lt;T&gt;&amp;, const SimpleAllocator&lt;U&gt;&amp;);</del>
</pre>
</blockquote>

</li>

<li><p>Edit 20.2.9 <a href="https://wg21.link/allocator.traits">[allocator.traits]</a>p1, class template <code>allocator_traits</code> synopsis, as indicated:</p>

<blockquote><pre>
namespace std {
  template &lt;class Alloc&gt; struct allocator_traits {
    typedef Alloc allocator_type;

    typedef <del>typename Alloc::value_type</del><ins><i>see below</i></ins> value_type;

    [&hellip;]

    static Alloc select_on_container_copy_construction(const Alloc&amp; rhs);

    <ins>static bool equal(const Alloc&amp; a1, const Alloc&amp; a2) noexcept;</ins>
  };
}
</pre></blockquote>
</li>

<li><p>At the beginning of 20.2.9.2 <a href="https://wg21.link/allocator.traits.types">[allocator.traits.types]</a>, add a new paragraph:</p>

<blockquote><pre>
<ins>typedef <i>see below</i> value_type;</ins>
</pre><blockquote>
<p>
<ins><i>Type:</i> <code>Alloc::value_type</code> if such a type exists; otherwise, <code>T</code> if <code>Alloc</code> is a class template 
instantiation of the form <code>Alloc&lt;T, Args&gt;</code>, where <code>Args</code> is zero or more type arguments; otherwise, the program 
is ill-formed.</ins>
</p>
</blockquote></blockquote>
</li>

<li><p>At the end of 20.2.9.3 <a href="https://wg21.link/allocator.traits.members">[allocator.traits.members]</a>, add a new paragraph:</p>

<blockquote><pre>
<ins>static bool equal(const Alloc&amp; a1, const Alloc&amp; a2) noexcept;</ins>
</pre><blockquote>
<p>
<ins>-?- <i>Returns:</i> <code>a1 == a2</code> if that expression is well-formed; otherwise, <code>true</code>.</ins>
</p>
</blockquote></blockquote>
</li>
</ol>





</body>
</html>
