title: C++ zero-cost abstraction for SoA/AoS memory layouts
body: <p>Say I have a large code using Array of Structures (AoS) memory layout. I would like to build a zero-cost abstraction in C++ which allows me to switch between AoS and SoA with as little refactoring effort as possible.
  For instance take a class with access member functions</p>
  
  <pre><code> struct Item{
     auto&amp; myDouble(){ return mDouble; }
     auto&amp; myChar(){ return mChar; }
     auto&amp; myString(){ return mString; }
   private:
     double mDouble;
     char mChar;
     std::string mString;
   };
  </code></pre>
  
  <p>which is used inside a container in a loop</p>
  
  <pre><code>std::vector&lt;Item&gt; vec_(1000);
  for (auto&amp; i : vec_)
    i.myDouble()=5.;
  </code></pre>
  
  <p>I would like to change the first snippet while the second one remains similar.. e.g. having something like</p>
  
  <pre><code>MyContainer&lt;Item, SoA&gt; vec_(1000)
  for (auto&amp; i : vec_)
    i.myDouble()=5.;
  </code></pre>
  
  <p>in which I can select the memory layout with a "SoA" or "AoS" template parameters. My questions are: does such thing exist somewhere? And if it doesn't, how would it be implemented at best?</p>