<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="" xml:lang="">
<head>
  <meta charset="utf-8" />
  <meta name="generator" content="pandoc" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes" />
  <meta name="author" content="Pablo Halpern &lt;phalpern@halpernwightsoftware.com&gt;" />
  <title>P2652R1: Disallow User Specialization of allocator_traits</title>
  <style type="text/css">
      code{white-space: pre-wrap;}
      span.smallcaps{font-variant: small-caps;}
      span.underline{text-decoration: underline;}
      div.column{display: inline-block; vertical-align: top; width: 50%;}
  </style>
</head>
<body>
<header id="title-block-header">
<h1 class="title">P2652R1: Disallow User Specialization of <code>allocator_traits</code></h1>
<p class="author">Pablo Halpern &lt;<a href="mailto:phalpern@halpernwightsoftware.com" class="email">phalpern@halpernwightsoftware.com</a>&gt;</p>
<p class="date"><!-- $TimeStamp$ -->2022-11-11 09:32 HST<!-- $ --></p>
</header>
<p>Target audience: LWG</p>
<head>
<style type="text/css">
  body { max-width: 54em; }
  ins { text-decoration:none; background-color:#A0FFA0 }
  .new { text-decoration:none; background-color:#D0FFD0 }
  del { text-decoration:line-through; background-color:#FFA0A0 }
  strong { font-weight: inherit; color: #2020ff }
  table, td, th { border: 1px solid black; border-collapse:collapse; padding: 5px }
  blockquote { margin: 1em 0 1em 1.7em; padding-left: 1em; border-left: 0; }
  pre { left-margin: 50px; line-height: 1.1; }
  pre code { font-size: 80%; }
  del pre code { text-decoration: inherit; background-color:#FFA0A0 }
  ins pre code { text-decoration: inherit; background-color:#A0FFA0 }
</style>
</head>
<h1 id="abstract">Abstract</h1>
<p>The <code>allocator_traits</code> class template was introduced in C++11 with two goals in mind: 1) Provide default implementations for allocator types and operations, thus minimizing the requirements on allocators [allocator.requirements], and 2) provide a mechanism by which future standards could extend the allocator interface without changing allocator requirements and thus obsoleting existing allocators. The latter goal is undermined, however, by the standard currently allowing user-defined specializations of <code>std::allocator_traits</code>. Although the standard requires that any such specialization conform to the standard interface, it is not practical to change the standard interface – even by extending it – without breaking any existing user specializations. Indeed, the Sep 2022 C++23 CD, <a href="https://www.open-std.org/JTC1/SC22/WG21/prot/14882fdis/n4919.pdf">N4919</a> contains an extension, <code>allocate_at_least</code>, that logically belongs in <code>std:::allocator_traits</code>, but is expressed as an unrelated function because of the problem of potential user-defined specializations.</p>
<p>This paper proposes that the standard remove the user’s latitude for specializing <code>std::allocator_traits</code>.</p>
<p>This paper is the proposed resolution to a US NB comment having the same title; it is targeted for C++23.</p>
<h1 id="change-history-r0---r1">Change History R0 -&gt; R1</h1>
<p>R0 was reviewed by LEWG at the Kona meeting on 2022-11-09 and the first proposed resolution (of the two PRs in R0) was forwarded to LWG with no changes. LWG asked for the changes in this version on 2022-11-10 during the same Kona meeting.</p>
<ul>
<li>There was no interest in LEWG in proposed resolution 2, so it was removed.</li>
<li>Changed the count in <code>allocation_result</code> from <code>std::size_t</code> to the allocator’s <code>size_type</code>. Although not directly related to this proposal, LWG noticed this oversight while reviewing the paper and asked for this change.</li>
<li>Changed size argument for <code>allocate_at_least</code> from <code>size_t</code> to <code>size_type</code>.</li>
</ul>
<h1 id="motivation">Motivation</h1>
<p>The minimal interface for a type conforming to the allocator requirements is that it have a <code>value_type</code> type, <code>allocate</code> and <code>deallocate</code> member functions, and equality comparison operators. The <code>allocator_traits</code> class template provides many other types and functions such as <code>pointer</code>, <code>rebind</code>, and <code>construct</code>. Generic types that use allocators are required to access the allocator through <code>std::allocator_traits</code>. The latter requirement was intended to allow the allocator interface to be extended without necessarily changing every existing allocator.</p>
<p>For example, C++03 allocators did not have a <code>void_pointer</code> member, but such a member is provided automatically through <code>allocator_traits</code>; an allocator class can override the default provided by <code>allocator_traits</code>, but is not required to do so.</p>
<p>The Standard description for each trait <code>X</code> in <code>std::allocator_traits&lt;A&gt;</code> typically follows the form, “<code>a.X</code> if that expression is well-formed; otherwise <em>some default</em>.” There is never any reason to specialize <code>std::allocator_traits</code> because any trait can be overridden simply by defining the appropriate member within the specific allocator class template. Unfortunately, the standard allows such user specialization and even implies that it is a reasonable thing to do. This allowance prevents the Standards Committee from adding new members to <code>std::allocator_traits</code> without breaking existing user specializations.</p>
<p>In <a href="http://wg21.link/P0401R1">P0401R1</a>, <code>allocate_at_least</code> was proposed as a static member of <code>std::allocator_traits</code> but it was changed to a free function in <a href="http://wg21.link/P0401R2">P0401R2</a> following a poll in LEWG in Cologne after it was pointed out that, because <code>std::allocator_traits</code> can be specialized and that existing specializations would not have the <code>allocate_at_least</code> member. It is this free function that is now in the September 2022 C++23 CD, <a href="https://www.open-std.org/JTC1/SC22/WG21/prot/14882fdis/n4919.pdf">N4919</a>. The current state of affairs, then, is that accessing an allocator is starting to become a hodgepodge of <code>std::allocator_traits</code> member-function calls and free-function calls. Before we standardize C++23, we should make an attempt to prevent this divergence.</p>
<h1 id="proposed-resolution">Proposed resolution</h1>
<p>This proposed resolution would disallow user specializations of <code>std::allocator_traits</code>. This change would be a breaking one, as existing specializations would become non-conforming. However, with the exception of the new <code>allocate_at_least</code> feature, existing code should continue to work for the time being. It is expected that specializations of <code>std::allocator_traits</code> are very rare, so the amount of potential breakage should be quite limited. Indeed, C++17 already added a member, <code>is_always_equal</code> to <code>std::allocator_traits</code>, theoretically breaking existing user-defined specializations but producing no outcry from the C++ community.</p>
<h2 id="wording">Wording</h2>
<p>Changes are relative to the 2022-09-05 Working Draft, <a href="https://www.open-std.org/JTC1/SC22/WG21/docs/papers/2022/n4917.pdf">N4917</a>:</p>
<p>Modify section 16.4.4.6.1 [allocator.requirements.general], paragraph 3 as follows:</p>
<blockquote>
<p>The class template <code>allocator_traits</code> (20.2.9) supplies a uniform interface to all allocator types. This subclause describes the requirements on allocator types and thus on types used to instantiate <code>allocator_traits</code>. A requirement is optional if a default for a given type or expression is specified. Within the standard library <code>allocator_traits</code> template, an optional requirement that is not supplied by an allocator is replaced by the specified default type or expression. <del>A user specialization of allocator_traits may provide different defaults and may provide defaults for different requirements than the primary template.</del> <ins>[ <em>Note</em>: There are no program-defined specializations of <code>allocator_traits</code>. – <em>end note</em> ]</p>
</blockquote>
<p>And Paragraphs 43 to 46 as follows:</p>
<pre><code>a.allocate_at_least(n)</code></pre>
<blockquote>
<p><em>Result</em>: <code>allocation_result&lt;X::pointer</code><ins><code>, X::size_type</code></ins><code>&gt;</code></p>
</blockquote>
<blockquote>
<p><em>Returns</em>: <code>allocation_result&lt;X::pointer</code><ins><code>, X::size_type</code></ins><code>&gt;{ptr, count}</code> where <code>ptr</code> is memory allocated for an array of <code>count T</code> and such an object is created but array elements are not constructed, such that <code>count &gt;= n</code>. If <code>n == 0</code>, the return value is unspecified.</p>
</blockquote>
<blockquote>
<p><em>Throws</em>: <code>allocate_at_least</code> may throw an appropriate exception.</p>
</blockquote>
<blockquote>
<p><em>Remarks</em>: <del>An allocator need not <code>support allocate_at_least</code>, but no default is provided in <code>allocator_traits</code>. If an allocator has an <code>allocate_at_least</code> member, it shall satisfy the requirements.</del> <ins>Default: <code>{a.allocate(n), n}</code>.</ins></p>
</blockquote>
<p>In section 20.2.2 [memory.syn], add a <code>SizeType</code> parameter to <code>allocation_result</code> and remove the non-member declaration for <code>allocate_at_least</code>:</p>
<p><code>template&lt;class Pointer</code><ins><code>, class SizeType = size_t</code></ins><code>&gt;</code><br />
<code>struct allocation_result {</code><br />
   <code>Pointer ptr;</code><br />
   <del><code>size_t</code></del><ins><code>SizeType</code></ins> <code>count;</code><br />
<code>};</code></p>
<del>
<pre><code>template&lt;class Allocator&gt;
  [[nodiscard]] constexpr allocation_result&lt;typename allocator_traits&lt;Allocator&gt;::pointer&gt;
    allocate_at_least(Allocator&amp; a, size_t n);                  // freestanding</code></pre>
</del>
<p>In section 20.2.9.1 [allocator.traits.general], amend the introductory paragraph:</p>
<blockquote>
<p>The class template <code>allocator_traits</code> supplies a uniform interface to all allocator types. An allocator cannot be a non-class type, however, even if allocator_traits supplies the entire required interface. <ins>If a program declares an explicit or partial specialization of <code>allocator_traits</code>, the program is ill-formed, no diagnostic required.</ins></p>
</blockquote>
<p>Within the class definition, add a new member to <code>allocator_traits</code>, probably immediately before <code>deallocate</code>:</p>
<ins>
<pre><code>template&lt;class Allocator&gt;
  [[nodiscard]] static constexpr allocation_result&lt;pointer, size_type&gt;
    allocate_at_least(Allocator&amp; a, size_type n);</code></pre>
</ins>
<p>And in section 20.2.9.3 [allocator.traits.members], add its definition</p>
<ins>
<pre><code>template&lt;class Allocator&gt;
  [[nodiscard]] static constexpr allocation_result&lt;pointer, size_type&gt;
    allocate_at_least(Allocator&amp; a, size_type n);</code></pre>
</ins>
<blockquote>
<ins>
<em>Returns</em>: <code>a.allocate_at_least(n)</code> if that expression is well-formed; otherwise, <code>{a.allocate(n), n}</code>.
</ins>
</blockquote>
<p>In section 20.2.9.4 [allocator.traits.other] paragraph 2, remove the definition of non-member <code>allocate_at_least</code>:</p>
<del>
<pre><code>template&lt;class Allocator&gt;
  [[nodiscard]] constexpr allocation_result&lt;typename allocator_traits&lt;Allocator&gt;::pointer&gt;
    allocate_at_least(Allocator&amp; a, size_t n);</code></pre>
</del>
<blockquote>
<del>
<em>Returns</em>: <code>a.allocate_at_least(n)</code> if that expression is well-formed; otherwise, <code>{a.allocate(n), n}</code>.
</del>
</blockquote>
<p>Finally, add a new subclause “Clause 20: memory management library [diff.cpp20.alloc.traits]” between [diff.cpp20.concepts] and [diff.cpp20.utilities] in Annex C:</p>
<blockquote>
<ins>
<strong>Affected subclause</strong>: [allocator.traits.general]
</ins>
</blockquote>
<blockquote>
<ins>
<strong>Change</strong>: Forbid partial and explicit program-defined specializations of <code>allocator_traits</code>.
</ins>
</blockquote>
<blockquote>
<ins>
<strong>Rationale</strong>: Allow addition of <code>allocate_at_least</code> to <code>allocator_traits</code>, and potentially other members in the future.
</ins>
</blockquote>
<blockquote>
<ins>
<strong>Effect on original feature</strong>: Valid C++ 2020 code that partially or explicitly specializes <code>allocator_traits</code> is ill-formed with no diagnostic required in this revision of C++.
</ins>
</blockquote>
<h1 id="conclusion">Conclusion</h1>
<p>The current working draft has allocator operations specified as a mixture of <code>allocator_traits</code> members and namespace-scope traits. If we want to prevent this divergence, then user specializations must be disallowed in C++23, <em>before</em> the standard starts drifting in the that direction.</p>
<h1 id="references">References</h1>
<p><a href="https://www.open-std.org/JTC1/SC22/WG21/docs/papers/2022/n4917.pdf">N4917</a>: Working Draft, Programming Languages – C++, 2022-09-05.</p>
<p><a href="https://www.open-std.org/JTC1/SC22/WG21/prot/14882fdis/n4919.pdf">N4919</a>: Programming Languages – C++, Committee Draft, 2022-09-05.</p>
<p><a href="http://wg21.link/P0401">P0401R6</a>: Providing size feedback in the Allocator interface, Jonathan Wakely and Chris Kennelly, 2021-01-22</p>
</body>
</html>
