title: Modern way to access struct fields by name in C++
body: <p>I wanted to check if there's an intuitive and easy way to access struct fields by name in modern C++.
  I am  aware that similar questions have been asked and answered, and C++ reflection is a well investigated subject.
  I've came across libraries like:</p>
  <ul>
  <li><a href="https://fossies.org/linux/boost/libs/hana/example/tutorial/introspection.json.cpp" rel="nofollow noreferrer">boost-hana</a></li>
  <li><a href="https://bytemaster.github.io/boost_reflect/" rel="nofollow noreferrer">boost-reflect</a></li>
  <li><a href="https://github.com/garbageslam/visit_struct" rel="nofollow noreferrer">visit_struct</a></li>
  <li><a href="https://github.com/apolukhin/magic_get" rel="nofollow noreferrer">magic_get</a>:</li>
  </ul>
  <p>But the common point in all these approaches is that, they only allow you to get the total number of fields within the struct or do a certain operation in <code>for_each</code> manner for all the fields of the struct.</p>
  <p>Yes, I can obviously check for the specific &quot;name&quot; of the field I'm looking for, by using the <code>for_each</code> functionality provided by these libraries. But I just wanted to check if there is any other trivial/well-know library that already does this.
  I would like to be able to deal with arbitrary number of nested structs, which is why I'm looking for something out of the box.</p>
  <p>As Louis Go indicated, it would be great to have an accessor like:</p>
  <pre><code>auto field = namespace::getField&lt;mystruct&gt;(&quot;fieldname&quot;);
  </code></pre>