Introspecting struct definition in C++?
<p>So I'm working on a file format for storing structured data defined over some kind of range.  The simplest example would be something like float values over time, but I'm designing it to allow multiple dimensions and arbitrary data at each point. I'd like to store the structure of the data in the file header as well because it will allow for some neat features.  </p>

<p>At first I thought I'd have the user build up their data structure using a hierarchy of classes, so you could do something like this:</p>

<pre><code>pf_type data = record(PF_DOUBLE) (
                  record("position")(
                     field("xp") &amp;
                     field("yp") &amp;
                     field("zp")) &amp;
                  record("velocity")(
                     field("xv") &amp;
                     field("yv") &amp;
                     field("zv")) &amp;
                  record("acceleration")(
                     field("xa") &amp;
                     field("ya") &amp;
                     field("za")));
</code></pre>

<p>which would be equivalent to something like this in C:</p>

<pre><code>struct {
   struct position {
       double xp,yp,zp;
   }
   struct velocity {
       double xv,yv,zv;
   }
   struct acceleration {
       double xa,ya,za;
   }
}
</code></pre>

<p>It's not terrible, but it still requires the user to separately define a c struct to actually use when reading/writing the data. </p>

<p>I thought it'd be neat if they could just define a regular c-struct that they passed to my library and I introspected upon it to get the information to write to the file.  Though I have no idea if something like that is remotely possible in C++.  My thought is that it might be using some template metaprogramming magic but that it'd be extremely messy.  So I thought I'd solicit some ideas from those that know more about C++ than I do.</p>
