<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 768: Typos in [atomics]?</title>
<meta property="og:title" content="Issue 768: Typos in [atomics]?">
<meta property="og:description" content="C++ library issue. Status: CD1">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue768.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#CD1">CD1</a> status.</em></p>
<h3 id="768"><a href="lwg-defects.html#768">768</a>. Typos in [atomics]?</h3>
<p><b>Section:</b> 32.5.8 <a href="https://wg21.link/atomics.types.generic">[atomics.types.generic]</a> <b>Status:</b> <a href="lwg-active.html#CD1">CD1</a>
 <b>Submitter:</b> Alberto Ganesh Barbati <b>Opened:</b> 2007-12-28 <b>Last modified:</b> 2016-01-28</p>
<p><b>Priority: </b>Not Prioritized
</p>
<p><b>View all other</b> <a href="lwg-index.html#atomics.types.generic">issues</a> in [atomics.types.generic].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#CD1">CD1</a> status.</p>
<p><b>Discussion:</b></p>
<p>
in the latest publicly available draft, paper
<a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2461.pdf">N2641</a>,
in section 32.5.8 <a href="https://wg21.link/atomics.types.generic">[atomics.types.generic]</a>, the following specialization of the template
<code>atomic&lt;&gt;</code> is provided for pointers:
</p>

<blockquote><pre>
template &lt;class T&gt; struct atomic&lt;T*&gt; : atomic_address { 
  T* fetch_add(ptrdiff_t, memory_order = memory_order_seq_cst) volatile; 
  T* fetch_sub(ptrdiff_t, memory_order = memory_order_seq_cst) volatile; 

  atomic() = default; 
  constexpr explicit atomic(T); 
  atomic(const atomic&amp;) = delete; 
  atomic&amp; operator=(const atomic&amp;) = delete; 

  T* operator=(T*) volatile; 
  T* operator++(int) volatile; 
  T* operator--(int) volatile; 
  T* operator++() volatile; 
  T* operator--() volatile; 
  T* operator+=(ptrdiff_t) volatile;
  T* operator-=(ptrdiff_t) volatile; 
};
</pre></blockquote>

<p>
First of all, there is a typo in the non-default constructor which
should take a <code>T*</code> rather than a <code>T</code>.
</p>

<p>
As you can see, the specialization redefine and therefore hide a few
methods from the base class <code>atomic_address</code>, namely <code>fetch_add</code>, <code>fetch_sub</code>,
<code>operator=</code>, <code>operator+=</code> and <code>operator-=</code>. That's good, but... what happened
to the other methods, in particular these ones:
</p>

<blockquote><pre>
void store(T*, memory_order = memory_order_seq_cst) volatile;
T* load( memory_order = memory_order_seq_cst ) volatile;
T* swap( T*, memory_order = memory_order_seq_cst ) volatile;
bool compare_swap( T*&amp;, T*, memory_order, memory_order ) volatile;
bool compare_swap( T*&amp;, T*, memory_order = memory_order_seq_cst ) volatile;
</pre></blockquote>

<p>
By reading paper
<a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2427.html">N2427 "C++ Atomic Types and Operations"</a>,
I see that the
definition of the specialization <code>atomic&lt;T*&gt;</code> matches the one in the
draft, but in the example implementation the methods <code>load()</code>, <code>swap()</code>
and <code>compare_swap()</code> are indeed present.
</p>

<p>
Strangely, the example implementation does not redefine the method
<code>store()</code>. It's true that a <code>T*</code> is always convertible to <code>void*</code>, but not
hiding the <code>void*</code> signature from the base class makes the class
error-prone to say the least: it lets you assign pointers of any type to
a <code>T*</code>, without any hint from the compiler.
</p>

<p>
Is there a true intent to remove them from the specialization or are
they just missing from the definition because of a mistake?
</p>

<p><i>[
Bellevue:
]</i></p>


<blockquote>
<p>
The proposed revisions are accepted.
</p>
<p>
Further discussion: why is the ctor labeled "constexpr"? Lawrence said
this permits the object to be statically initialized, and that's
important because otherwise there would be a race condition on
initialization.
</p>
</blockquote>


<p id="res-768"><b>Proposed resolution:</b></p>
<p>
Change the synopsis in 32.5.8 <a href="https://wg21.link/atomics.types.generic">[atomics.types.generic]</a>:
</p>

<blockquote><pre>
template &lt;class T&gt; struct atomic&lt;T*&gt; : atomic_address { 
  <ins>void store(T*, memory_order = memory_order_seq_cst) volatile;</ins>
  <ins>T* load( memory_order = memory_order_seq_cst ) volatile;</ins>
  <ins>T* swap( T*, memory_order = memory_order_seq_cst ) volatile;</ins>
  <ins>bool compare_swap( T*&amp;, T*, memory_order, memory_order ) volatile;</ins>
  <ins>bool compare_swap( T*&amp;, T*, memory_order = memory_order_seq_cst ) volatile;</ins>

  T* fetch_add(ptrdiff_t, memory_order = memory_order_seq_cst) volatile; 
  T* fetch_sub(ptrdiff_t, memory_order = memory_order_seq_cst) volatile; 

  atomic() = default; 
  constexpr explicit atomic(T<ins>*</ins>); 
  atomic(const atomic&amp;) = delete; 
  atomic&amp; operator=(const atomic&amp;) = delete; 

  T* operator=(T*) volatile; 
  T* operator++(int) volatile; 
  T* operator--(int) volatile; 
  T* operator++() volatile; 
  T* operator--() volatile; 
  T* operator+=(ptrdiff_t) volatile;
  T* operator-=(ptrdiff_t) volatile; 
};
</pre></blockquote>






</body>
</html>
