<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 3304: Allocate functions of std::polymorphic_allocator should require [[nodiscard]]</title>
<meta property="og:title" content="Issue 3304: Allocate functions of std::polymorphic_allocator should require [[nodiscard]]">
<meta property="og:description" content="C++ library issue. Status: C++20">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue3304.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++20">C++20</a> status.</em></p>
<h3 id="3304"><a href="lwg-defects.html#3304">3304</a>. Allocate functions of <code>std::polymorphic_allocator</code> should require <code>[[nodiscard]]</code></h3>
<p><b>Section:</b> 20.5.3 <a href="https://wg21.link/mem.poly.allocator.class">[mem.poly.allocator.class]</a> <b>Status:</b> <a href="lwg-active.html#C++20">C++20</a>
 <b>Submitter:</b> Hiroaki Ando <b>Opened:</b> 2019-10-16 <b>Last modified:</b> 2021-02-25</p>
<p><b>Priority: </b>3
</p>
<p><b>View all other</b> <a href="lwg-index.html#mem.poly.allocator.class">issues</a> in [mem.poly.allocator.class].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#C++20">C++20</a> status.</p>
<p><b>Discussion:</b></p>
<p>
<code>[[nodiscard]]</code> is specified for <code>std::polymorphic_allocator&lt;&gt;::allocate()</code>.
<p/>
But the allocate functions added with <a href="https://wg21.link/p0339r6">P0339R6</a> doesn't have it.
<p/>
Isn't <code>[[nodiscard]]</code> necessary for these functions?
</p>

<p><i>[2019-11 Priority to 3 during Monday issue prioritization in Belfast]</i></p>

<p><i>[2019-11 After discussion with LEWG, assigning to LEWG]</i></p>

<p><i>[2019-11-4; Daniel comments]</i></p>

<p>
This issue is related to LWG <a href="lwg-closed.html#3312" title="polymorphic_allocator::allocate_object and new_object should be [[nodiscard]] (Status: Dup)">3312</a><sup><a href="https://cplusplus.github.io/LWG/issue3312" title="Latest snapshot">(i)</a></sup>.
</p>
<p><i>[2019-11; Friday AM in Belfast. Status changed to "Ready"]</i></p>



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

<ol>
<li><p>Modify 20.5.3 <a href="https://wg21.link/mem.poly.allocator.class">[mem.poly.allocator.class]</a>, class template <code>polymorphic_allocator</code> 
synopsis, as indicated:</p>

<blockquote>
<pre>
namespace std::pmr {
  template&lt;class Tp = byte&gt; class polymorphic_allocator {
    [&hellip;]
    <i>// 20.5.3.3 <a href="https://wg21.link/mem.poly.allocator.mem">[mem.poly.allocator.mem]</a>, member functions</i>
    [[nodiscard]] Tp* allocate(size_t n);
    void deallocate(Tp* p, size_t n);

    <ins>[[nodiscard]]</ins> void* allocate_bytes(size_t nbytes, size_t alignment = alignof(max_align_t));
    void deallocate_bytes(void* p, size_t nbytes, size_t alignment = alignof(max_align_t));
    template&lt;class T&gt; <ins>[[nodiscard]]</ins> T* allocate_object(size_t n = 1);
    template&lt;class T&gt; void deallocate_object(T* p, size_t n = 1);
    template&lt;class T, class... CtorArgs&gt; <ins>[[nodiscard]]</ins> T* new_object(CtorArgs&amp;&amp;... ctor_args);
    template&lt;class T&gt; void delete_object(T* p);  
    [&hellip;]
  };  
}
</pre>
</blockquote>
</li>

<li><p>Modify 20.5.3.3 <a href="https://wg21.link/mem.poly.allocator.mem">[mem.poly.allocator.mem]</a> as indicated:</p>

<blockquote>
<pre>
<ins>[[nodiscard]]</ins> void* allocate_bytes(size_t nbytes, size_t alignment = alignof(max_align_t));
</pre>
<blockquote>
<p>
-5- <i>Effects:</i> Equivalent to: <code>return memory_rsrc-&gt;allocate(nbytes, alignment);</code>
<p/>
[&hellip;]
</p>
</blockquote>
[&hellip;]
<pre>
template&lt;class T&gt;
  <ins>[[nodiscard]]</ins> T* allocate_object(size_t n = 1);
</pre>
<blockquote>
<p>
-8- <i>Effects:</i> Allocates memory suitable for holding an array of <code>n</code> objects of type <code>T</code>, as follows:
<ol style="list-style-type: none">
<li><p>(8.1) &mdash; if <code>SIZE_MAX / sizeof(T) &lt; n</code>, throws <code>length_error</code>,</p></li>
<li><p>(8.2) &mdash; otherwise equivalent to:</p>
<blockquote><pre>
return static_cast&lt;T*&gt;(allocate_bytes(n*sizeof(T), alignof(T)));
</pre></blockquote>
</li>
</ol>
<p/>
[&hellip;]
</p>
</blockquote>
<pre>
template&lt;class T, class CtorArgs...&gt;
  <ins>[[nodiscard]]</ins> T* new_object(CtorArgs&amp;&amp;... ctor_args);
</pre>
<blockquote>
<p>
-11- <i>Effects:</i> Allocates and constructs an object of type <code>T</code>, as follows.
Equivalent to:
<blockquote><pre>
T* p = allocate_object&lt;T&gt;();
try {
  construct(p, std::forward&lt;CtorArgs&gt;(ctor_args)...);
} catch (...) {
  deallocate_object(p);
  throw;
}
return p;
</pre></blockquote>
<p/>
[&hellip;]
</p>
</blockquote>
</blockquote>
</li>
</ol>





</body>
</html>
