title: C++ std::atomic union
body: <p>How do I set a union to be atomic using std::atomic?  Or do I have to declare the members of the union to be atomic instead?</p>
  
  <pre><code>typedef union {
      int integer;
      float flt;
      double dbl;
      int *intArray;
      float *floatArray;
      unsigned char *byteArray;
  } ValueUnion;
  
  class FooClass {
  public:
      std::atomic&lt;ValueUnion&gt; value;
  
  }; 
  </code></pre>
  
  <p>Access to the union gives an error:</p>
  
  <pre><code>foo-&gt;value.floatArray = NULL;
  
  error: no member named 'floatArray' in 'std::__1::atomic&lt;ValueUnion&gt;'
                      foo-&gt;value.floatArray = NULL;
  </code></pre>
  
  <p>Do I need to do something like:</p>
  
  <pre><code>typedef union {
      std::atomic&lt;int&gt; integer;
      std::atomic&lt;float&gt; flt;
      std::atomic&lt;double&gt; dbl;
      std::atomic&lt;int*&gt; *intArray;
      std::atomic&lt;float*&gt; *floatArray;
      std::atomic&lt;unsigned char*&gt; *byteArray;
  } ValueUnion;
  </code></pre>
  
  <p>and declare the member variable value to be as below?</p>
  
  <pre><code>class FooClass {
  public:
      ValueUnion value;
  
  }; 
  </code></pre>