Iterate through struct by variable name
<p>UPDATE: 6 months later, and I just came across this answer: <a href="https://stackoverflow.com/a/40590471/2066079">Is it legal to index into a struct?: Answer by Slava</a> . I think this is a MUCH better solution than any of the ones provided here, as there is absolutely no undefined behavior. Hopefully this helps the next person, since it is already too late for me to implement.</p>

<hr>

<p><strong>Before you comment telling me to use an array or vector, or any form of container instead, it is a hard truth that I cannot. I know, this would be solved with an array, and any solution otherwise is pretty "hacky". I would love to use a container, but I absolutely cannot.</strong></p>

<p>I am a mid-level developer at a very large corporation, and we are using a company-wide library for sending data over ethernet. There are various reasons for why it cannot support arrays/vectors, and instead, uses structs of POD (Plain Old Data - chars, floats, ints, bools). I start with an array of floats that I must use to fill a struct with the same number of floats. Since the purpose of this library is to send messages over ethernet, I only need to do the iteration twice - once on the send and one on the receive. All other times, this data is stored as an array. I know - I should be serializing the arrays and sending them as is, but I repeat - I absolutely cannot. </p>

<p>I have a <code>float[1024]</code>, and must iterate through the array and fill the following struct:</p>

<pre><code>struct pseudovector
{
    float data1;
    float data2;
    float data3;
    ...
    float data1024;
}
</code></pre>

<p>I am already generating this struct with <a href="https://stackoverflow.com/questions/18441431/boost-pp-repeat-with-array">BOOST_PP_REPEAT</a> and <a href="https://stackoverflow.com/questions/18483572/how-can-i-use-a-macro-for-collecting-variable-names/18483867#18483867">BOOST_PP_SEQ_FOR_EACH_I</a> so that I do not have to write out all 1024 floats, and it increases maintainability/extensibility.</p>

<p>In the same fashion, I have tried iterating through the struct via pre-compiler <code>##</code> concatination (<a href="https://stackoverflow.com/a/29020943/2066079">https://stackoverflow.com/a/29020943/2066079</a>), but as this is done at pre-compiler time, it cannot be used for run-time getting/setting.</p>

<p>I have looked at implementing reflection such as <a href="https://stackoverflow.com/questions/41453/how-can-i-add-reflection-to-a-c-application">How can I add reflection to a C++ application?</a> and <a href="https://github.com/billyquith/ponder" rel="nofollow noreferrer">Ponder Library</a>, but both approaches requires you to explicitly write out each item that can be reflected upon. In that case, I might as well just create a <code>std::map&lt;string, float&gt;</code> and iterate in a for loop via string/integer concatenation: </p>

<pre><code>for(i=0;i&lt;1024;i++)
{
    array[i] = map.get(std::string("data")+(i+1))
}
</code></pre>

<p>Can anyone recommend a cleaner solution that does not require me to write out in excess of 1024 lines of code? Your help is appreciated!</p>

<p><strong>Again, I repeat - I absolutely cannot use an array/vector of any sort.</strong></p>