<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 2159: atomic_flag initialization</title>
<meta property="og:title" content="Issue 2159: atomic_flag initialization">
<meta property="og:description" content="C++ library issue. Status: C++14">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue2159.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++14">C++14</a> status.</em></p>
<h3 id="2159"><a href="lwg-defects.html#2159">2159</a>. <code>atomic_flag</code> initialization</h3>
<p><b>Section:</b> 32.5.10 <a href="https://wg21.link/atomics.flag">[atomics.flag]</a> <b>Status:</b> <a href="lwg-active.html#C++14">C++14</a>
 <b>Submitter:</b> Alberto Ganesh Barbati <b>Opened:</b> 2012-05-24 <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.flag">issues</a> in [atomics.flag].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#C++14">C++14</a> status.</p>
<p><b>Discussion:</b></p>

<p>
32.5.10 <a href="https://wg21.link/atomics.flag">[atomics.flag]</a>&#47;4 describes the <code>ATOMIC_FLAG_INIT</code>, but it's not quite clear about a 
couple of points:
</p>
<ol>
<li><p>
it's said that <code>ATOMIC_FLAG_INIT</code> "can be used to initialize an object of type <code>atomic_flag</code>" 
and the following example:
</p>
<blockquote><pre>
std::atomic_flag guard = ATOMIC_FLAG_INIT;
</pre></blockquote>
<p>
is presented. It's not clear whether the macro can also be used in the other initialization contexts:
</p>
<blockquote><pre>
std::atomic_flag guard ATOMIC_FLAG_INIT; 
std::atomic_flag guard {ATOMIC_FLAG_INIT};

struct A { std::atomic_flag flag; A(); };
A::A() : flag (ATOMIC_FLAG_INIT); 
A::A() : flag {ATOMIC_FLAG_INIT};
</pre></blockquote>
<p>
Please also note that examples are non-normative, according to the ISO directives, meaning that the wording 
presents no normative way to use the macro.
</p>
</li>

<li><p>
it's said that "It is unspecified whether an uninitialized <code>atomic_flag</code> object has an initial state 
of set or clear.". I believe the use of "uninitialized" is inappropriate. First of all, if an object is 
uninitialized it is obvious that we cannot assert anything about its state. Secondly, it doesn't address the 
following cases:
</p>
<blockquote><pre>
std::atomic_flag a; <i>// object is "initialized" by trivial default constructor</i>
std::atomic_flag a {}; <i>// object is value-initialized</i>
static std::atomic_flag a; <i>// object is zero-initialized</i>
</pre></blockquote>
<p>
strictly speaking a trivial constructor "initializes" the object, although it doesn't actually initialize the 
sub-objects.
</p></li>

<li><p>it's said that "For a static-duration object, that initialization shall be static.". Considering 
the following example:</p>
<blockquote><pre>
struct A
{
  A(); <i>// user-provided, not constexpr</i>

  std::atomic_flag flag = ATOMIC_FLAG_INIT;
  <i>// possibly other non-static data members</i>
};

static A a;
</pre></blockquote>
<p>
The object <code>a.flag</code> (as a sub-object of the object <code>a</code>) has static-duration, yet the initialization 
has to be dynamic because <code>A::A</code> is not <code>constexpr</code>.
</p>

</li>
</ol>

<p><i>[2012, Portland]</i></p>

<p>
We would like to be able to allow more initialisation contexts for example:
</p>
<ol>
<li>C struct</li>
<li>C++ constructor initializer-list</li>
</ol>
<p>
However we need further input from experts with implementation specific knowledge
to identify which additional contexts (if any) would be universally valid.
</p>
<p>
Moved to open
</p>

<p><i>[2013, Chicago]</i></p>

<p>
Move to Immediate, following review.
</p>
<p>
Some discussion over the explicit use of only copy initialization, and not direct initialization.  This is necessary to
allow the implementation of <code>atomic_flag</code> as an aggregate, and may be further reviewed in the future.
</p>
<p>
Accept for Working Paper
</p>



<p id="res-2159"><b>Proposed resolution:</b></p>
<p><i>[This wording is relative to N3376.]</i></p>


<p>Change 32.5.10 <a href="https://wg21.link/atomics.flag">[atomics.flag]</a>&#47;4 as follows:</p>

<blockquote><p>
The macro <code>ATOMIC_FLAG_INIT</code> shall be defined in such a way that it can be used to initialize an object of
type <code>atomic_flag</code> to the clear state.  <ins>The macro can be used in the form:</ins>
</p>
<blockquote><pre>
<ins>atomic_flag guard = ATOMIC_FLAG_INIT;</ins>
</pre></blockquote>
<p>
<ins>It is unspecified whether the macro can be used in other initialization contexts.</ins> For a <ins>complete</ins>
static-duration object, that initialization shall be static. <del>It is unspecified whether an uninitialized</del> 
<ins>Unless initialized with <code>ATOMIC_FLAG_INIT</code>, it is unspecified whether an</ins> <code>atomic_flag</code> 
object has an initial state of set or clear. <del><i>[ Example:</i></del>
</p>
<blockquote><pre>
<del>atomic_flag guard = ATOMIC_FLAG_INIT;</del>
</pre></blockquote>
<p>
<del>&mdash; <i>end example ]</i></del>
</p>
</blockquote>






</body>
</html>
