title: Template function to access struct members
body: <p>I have a struct which contains other structs as well as primative data types. I'd like to be able to access each member including members of the structs contained within the main struct with a template function.</p>
  
  <p>For example:</p>
  
  <pre><code>struct B {
      int y;
      int z;
  };
  
  struct A {
      int x;
      B b;
  };
  
  template &lt;typename TMember&gt;
  bool doSomething(A *a, TMember member) {
  
      a-&gt;*member = 5;  // some code accessing member
  
  }
  
  // Then access it with:
  
  doSomething(&amp;myA, &amp;A::x);
  
  // and likewise
  
  doSomething(&amp;myA, &amp;A::b.y);
  </code></pre>
  
  <p>However the 2nd will not compile and throw a "object missing in reference to" error. I assume its because A does not have a member b.y? </p>
  
  <p>Is there anyway to get the functionality that I want or will coding a different function be required?</p>
  
  <p>(Note this is just an example and the code that I have contains a larger structure and will save me more time than just writing a second function.)</p>
  
  <p>Thanks!</p>