title: Switching back and forth between Array of Structures (AoS) and Structure of Arrays (SoA)
body: <p>One feature that plays a prominent role in many of the writings on data oriented design is that there are many cases where rather than AoS (array of structs):</p>
  
  <pre><code>struct C_AoS {
    int    foo;
    double bar;
  };
  
  std::vector&lt;C_AoS&gt; cs;
  ...
  std::cout &lt;&lt; cs[42].foo &lt;&lt; std::endl;
  </code></pre>
  
  <p>it is more efficient to arrange one's data in SoA (struct of arrays):</p>
  
  <pre><code>struct C_SoA {
    std::vector&lt;int&gt;    foo;
    std::vector&lt;double&gt; bar;
  };
  
  C_SoA cs;
  ...
  std::cout &lt;&lt; cs.foo[42] &lt;&lt; std::endl;
  </code></pre>
  
  <p>Now what I am looking for is a solution which would allow me to switch between AoS and SoA without changing the calling interface, i.e. that I could, with minimal effort and with no extra runtime cost (at least to the point of excessive indirection), call e.g. <code>cs[42].foo;</code> regardless of which arrangement of data I'm using. </p>
  
  <p>I should note that the example syntax above is the ideal case, which might very well be impossible, but I'd be very interested in close approximations, too. Any takers?</p>