<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 4194: atomic&lt;void*&gt; should use generic class template</title>
<meta property="og:title" content="Issue 4194: atomic&lt;void*&gt; should use generic class template">
<meta property="og:description" content="C++ library issue. Status: Tentatively NAD">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue4194.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">Tentatively NAD</a> status.</em></p>
<h3 id="4194"><a href="lwg-active.html#4194">4194</a>. <code>atomic&lt;void*&gt;</code> should use generic class template</h3>
<p><b>Section:</b> 32.5.8.5 <a href="https://wg21.link/atomics.types.pointer">[atomics.types.pointer]</a> <b>Status:</b> <a href="lwg-active.html#NAD">Tentatively NAD</a>
 <b>Submitter:</b> Gonzalo Brito <b>Opened:</b> 2025-01-16 <b>Last modified:</b> 2025-02-07</p>
<p><b>Priority: </b>Not Prioritized
</p>
<p><b>View other</b> <a href="lwg-index-open.html#atomics.types.pointer">active issues</a> in [atomics.types.pointer].</p>
<p><b>View all other</b> <a href="lwg-index.html#atomics.types.pointer">issues</a> in [atomics.types.pointer].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#Tentatively NAD">Tentatively NAD</a> status.</p>
<p><b>Discussion:</b></p>
<p>
32.5.8.5 <a href="https://wg21.link/atomics.types.pointer">[atomics.types.pointer]</a> p1 states (emphasis mine):
</p>
<blockquote style="border-left: 3px solid #ccc;padding-left: 15px;">
<p>
There is a partial specialization of the <code class='backtick'>atomic</code> class template for <b>pointers</b>.
</p>
</blockquote>
<p>
which requires <code>atomic&lt;void*&gt;</code> to use the <code class='backtick'>atomic</code> class template for pointers.
However, the <code class='backtick'>fetch_add</code>/<code class='backtick'>_sub</code> member functions add a <code class='backtick'>difference_type</code> to a <code class='backtick'>T*</code> 
which requires a pointer-to-object type (these member functions are <code class='backtick'>constexpr</code>, 
so trying to support this seems unimplementable).
<p/>
For <code class='backtick'>atomic_ref</code>, the 32.5.7.5 <a href="https://wg21.link/atomics.ref.pointer">[atomics.ref.pointer]</a> p1 states (emphasis mine):
</p>
<blockquote style="border-left: 3px solid #ccc;padding-left: 15px;">
<p>
There are specializations of the <code class='backtick'>atomic_ref</code>`  class template for all <b>pointer-to-object types</b>.
</p>
</blockquote>
<p>
which avoids this issue and applying the same form to 32.5.8.5 <a href="https://wg21.link/atomics.types.pointer">[atomics.types.pointer]</a> would make 
<code>atomic&lt;void*&gt;</code> and <code>atomic_ref&lt;void*&gt;</code> consistent.
<p/>
Technically this would be a breaking change, but all C++ standard library implementations surveyed are broken, 
and the proposed fix would make them compliant: see libstdc++, libc++ and MSVC STL errors 
<a href="https://godbolt.org/z/dso3P6fKf">here</a>. These standard libraries require a 
pointer-to-object type, <code>atomic&lt;void*&gt;</code> uses the general template. Therefore, no user 
code seems to be impacted.
</p>

<p><i>[2025-02-07; Reflector poll: NAD]</i></p>

<p>
The <code class='backtick'>fetch_OP</code> members have "<i>Mandates</i>: <code class='backtick'>T</code> is a complete object type."
and a note explaining that this means arithmetic on <code class='backtick'>void*</code> is ill-formed.
So implementations are expected to use the partial specialization for <code class='backtick'>void*</code>
but to reject attempts at arithmetic. They all do this correctly today.
</p>


<p id="res-4194"><b>Proposed resolution:</b></p>
<p>
This wording is relative to <a href="https://wg21.link/N5001" title=" Working Draft, Programming Languages — C++">N5001</a>.
</p>

<ol>

<li><p>Modify 32.5.8.5 <a href="https://wg21.link/atomics.types.pointer">[atomics.types.pointer]</a> as indicated:</p>

<blockquote><p>
-1- There is a partial specialization of the <code class='backtick'>atomic</code> class template for 
<del>pointers</del><ins>pointer-to-object types</ins>. Specializations of this partial
specialization are standard-layout structs. They each have a trivial destructor.
</p></blockquote>
</li>

</ol>





</body>
</html>
