title: Casting a Struct to an Array
body: <p>This is an strict aliasing question, as in will the compiler cause any optimization order problems with this.</p>
  
  <p>Say that I have three public <code>float</code>s in a <code>struct XMFLOAT3</code> (not unlike <a href="https://msdn.microsoft.com/en-us/library/windows/desktop/microsoft.directx_sdk.reference.xmfloat3%28v=vs.85%29.aspx" rel="nofollow">this one</a>.) And I want to cast to a <code>float*</code>. Will this land me in optimization trouble?</p>
  
  <pre><code>XMFLOAT3 foo = {1.0f, 2.0f, 3.0f};
  auto bar = &amp;foo.x;
  
  bar[2] += 5.0f;
  foo.z += 5.0f;
  cout &lt;&lt; foo.z;
  </code></pre>
  
  <p>I assume this will always print "13". But what about this code:</p>
  
  <pre><code>XMFLOAT3 foo = {1.0f, 2.0f, 3.0f};
  auto bar = reinterpret_cast&lt;float*&gt;(&amp;foo);
  
  bar[2] += 5.0f;
  foo.z += 5.0f;
  cout &lt;&lt; foo.z;
  </code></pre>
  
  <p>I believe this is legal because, according to <a href="http://en.cppreference.com/w/cpp/language/reinterpret_cast#Type_aliasing" rel="nofollow">http://en.cppreference.com/w/cpp/language/reinterpret_cast#Type_aliasing</a></p>
  
  <blockquote>
    <p>T2 is an aggregate type or a union type which holds one of the aforementioned types as an element or non-static member (including, recursively, elements of subaggregates and non-static data members of the contained unions): this makes it safe to cast from the first member of a struct and from an element of a union to the struct/union that contains it.</p>
  </blockquote>
  
  <p>Is my understanding of this correct?</p>
  
  <p><sub>Obviously this will become implementation dependent on the declaration of <code>XMFLOAT3</code>.</sub></p>