title: C++ iterate over members of struct
body: <p>Say I have a struct:</p>
  
  <pre><code>struct Boundary {
    int top;
    int left;
    int bottom;
    int right;
  }
  </code></pre>
  
  <p>and a vector </p>
  
  <pre><code>std::vector&lt;Boundary&gt; boundaries;
  </code></pre>
  
  <p>What would be the most C++ style way to access the structs to get the sum of <code>top</code>, <code>left</code>, <code>bottom</code> and <code>right</code> separately?</p>
  
  <p>I could write a loop like</p>
  
  <pre><code>for (auto boundary: boundaries) {
    sum_top+=boundary.top;
    sum_bottom+=boundary.bottom;
    ...
  }
  </code></pre>
  
  <p>This seems like a lot of repetition. Of course I could do this instead:</p>
  
  <pre><code>std::vector&lt;std::vector&lt;int&gt;&gt; boundaries;
  
  for (auto boundary: boundaries) {
    for(size_t i=0; i&lt;boundary.size();i++) {
      sums.at(i)+=boundary.at(i)
    }
  }
  </code></pre>
  
  <p>But then I'd loose all the meaningful struct member names. Is there a way so that I can write a something like the following function:</p>
  
  <pre><code>sum_top=make_sum(boundaries,"top");
  </code></pre>
  
  <p>Reflection does not seem to be an option in C++. I am open to use C++ up to Version 14.</p>