title: Can we use std::atomic<std::array<>>?
body: <p>As known: <a href="https://stackoverflow.com/a/32694707/1558037">https://stackoverflow.com/a/32694707/1558037</a></p>
  
  <p>Page 1104: <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3337.pdf" rel="nofollow noreferrer">http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3337.pdf</a></p>
  
  <p>C++11 §29.5/1 says</p>
  
  <blockquote>
    <p>There is a generic class template atomic. The type of the template
    argument T shall be trivially copyable (3.9).</p>
  </blockquote>
  
  <p>§3.9 tells</p>
  
  <blockquote>
    <p>Scalar types, trivially copyable class types (Clause 9), arrays of
    such types, and cv-qualified versions of these types (3.9.3) are
    collectively called trivially copyable types.</p>
  </blockquote>
  
  <p>Full text:</p>
  
  <pre><code>1 There is a generic class template atomic&lt;T&gt;. The type of the template argument T shall be trivially copyable (3.9). [ Note: Type arguments that are not also statically initializable may be difficult to use.
  — end note ]
  2 The semantics of the operations on specializations of atomic are defined in 29.6. 
  3 Specializations and instantiations of the atomic template shall have a deleted copy constructor, a deleted copy assignment operator, and a constexpr value constructor.
  4 There shall be full specializations of the atomic template for the integral types char, signed char, unsigned char, short, unsigned short, int, unsigned int, long, unsigned long, long long, unsigned long long, char16_t, char32_t, wchar_t, and any other types needed by the typedefs in the header &lt;cstdint&gt;. For each integral type integral, the specialization atomic&lt;integral&gt; provides additional atomic operations appropriate to integral types. There shall be a specialization atomic&lt;bool&gt; which provides the general atomic operations as specified in 29.6.1.
  5 The atomic integral specializations and the specialization atomic&lt;bool&gt; shall have standard layout. They shall each have a trivial default constructor and a trivial destructor. They shall each support aggregate initialization syntax.
  6 There shall be pointer partial specializations of the atomic class template. These specializations shall have standard layout, trivial default constructors, and trivial destructors. They shall each support aggregate initialization syntax.
  7 There shall be named types corresponding to the integral specializations of atomic, as specified in Table 145, and a named type atomic_bool corresponding to the specified atomic&lt;bool&gt;. Each named type is a either typedef to the corresponding specialization or a base class of the corresponding specialization. If it is a base class, it shall support the same member functions as the corresponding specialization.
  8 There shall be atomic typedefs corresponding to the typedefs in the header &lt;inttypes.h&gt; as specified in Table 146.
  9 [ Note: The representation of an atomic specialization need not have the same size as its corresponding argument type. Specializations should have the same size whenever possible, as this reduces the effort required to port existing code. — end note ]
  </code></pre>
  
  <p>Also this code shows that <code>std::array&lt;int, 100&gt;</code> is_trivially_copyable: <a href="http://coliru.stacked-crooked.com/a/712a445302406f68" rel="nofollow noreferrer">http://coliru.stacked-crooked.com/a/712a445302406f68</a></p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;type_traits&gt;
  #include &lt;array&gt;
  #include &lt;atomic&gt;
  
  int main() {
  
      std::array&lt;int, 100&gt; myarray;   // 100 elements
      std::cout &lt;&lt; "myarray - trivially_copyable: " &lt;&lt; std::boolalpha &lt;&lt;
          std::is_trivially_copyable&lt;decltype(myarray)&gt;::value &lt;&lt; std::endl;
  
      int c_array[100];
      std::cout &lt;&lt; "c_array - trivially_copyable: " &lt;&lt; std::boolalpha &lt;&lt;
          std::is_trivially_copyable&lt;decltype(c_array)&gt;::value &lt;&lt; std::endl;
  
      std::atomic&lt;std::array&lt;int, 100&gt;&gt; array_atomic;
          std::cout &lt;&lt; "sizeof(array_atomic): " &lt;&lt; std::boolalpha &lt;&lt;
          sizeof(decltype(array_atomic)) &lt;&lt; std::endl;
  
      //array_atomic.store(myarray);    // error
  
      //std::atomic&lt;c_array[100]&gt; c_array_atomic; // error
      //std::atomic&lt;decltype(c_array)&gt; c_array_atomic;    // error
  
  
      return 0;
  }
  </code></pre>
  
  <p>With result:</p>
  
  <pre><code>g++ -std=c++14 -O2 -Wall -pedantic -pthread main.cpp &amp;&amp; ./a.out
  myarray - trivially_copyable: true
  c_array - trivially_copyable: true
  sizeof(array_atomic): 400
  </code></pre>
  
  <p>But if I try to use <code>array_atomic.store(myarray);</code> then I get an error:</p>
  
  <pre><code>/tmp/ccgTvl0G.o: In function `main': main.cpp:(.text.startup+0xec):
  undefined reference to `__atomic_store' collect2: error: ld returned 1
  exit status
  </code></pre>
  
  <p>What also do we need for usage custom C++-type (class) as template of <code>std::atomic&lt;&gt;</code>?</p>
  
  <p><strong>ANSWER:</strong></p>
  
  <p>There should be new compilers and sometimes you need to link the library <code>-latomic</code></p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;array&gt;
  #include &lt;atomic&gt;
  
  int main() {
      std::array&lt;int, 100&gt; myarray;                   // 100 elements
      std::atomic&lt;std::array&lt;int, 100&gt;&gt; array_atomic;
      array_atomic.store(myarray);                    // atomic copy 100 elements
      return 0;
  }
  </code></pre>
  
  <p>It can be compiled and linked:</p>
  
  <ul>
  <li>ARM gcc 4.8.2 and higher <code>-std=c++11 -O3</code>: <a href="https://godbolt.org/g/tLRGD6" rel="nofollow noreferrer">https://godbolt.org/g/tLRGD6</a></li>
  <li>ARM64 gcc 4.8 and higher <code>-std=c++11 -O3</code>: <a href="https://godbolt.org/g/QFrZPZ" rel="nofollow noreferrer">https://godbolt.org/g/QFrZPZ</a></li>
  <li>PowerPC gcc 4.8 and higher <code>-std=c++11 -O3</code>: <a href="https://godbolt.org/g/9XP013" rel="nofollow noreferrer">https://godbolt.org/g/9XP013</a></li>
  <li>x86 clang 3.3 and higher <code>-std=c++11 -O3 -latomic</code>: <a href="https://godbolt.org/g/BdJsnb" rel="nofollow noreferrer">https://godbolt.org/g/BdJsnb</a></li>
  <li>x86 gcc 4.9.0 and higher <code>-std=c++11 -O3 -latomic</code>: <a href="https://godbolt.org/g/wifvCm" rel="nofollow noreferrer">https://godbolt.org/g/wifvCm</a></li>
  <li>x86 icc 13.0.1 and higher <code>-std=c++11 -O3</code>: <a href="https://godbolt.org/g/UIURSW" rel="nofollow noreferrer">https://godbolt.org/g/UIURSW</a></li>
  </ul>