title: How to design a class with "annotated" fields?
body: <p>Imagine we have some sort of protocol with hundreds of message types, each of which we want to model by a C++ class. Since each class should be able to process each field automatically, a natural solution is to just have an <code>std::tuple</code> with all the required types:</p>
  
  <pre><code>std::tuple&lt;int, double, char&gt; message;
  
  print(message);   // the usual variadic magic
  </code></pre>
  
  <p>This is all fine and well. However, now I want to give each field a name, and I want to be able to use the name when referring to the field in my code, as well as get a textual representation of it. Naively, or in C, I might have written:</p>
  
  <pre><code>struct Message
  {
      int    header;
      double temperature;
      char   flag;
  };
  </code></pre>
  
  <p>That way we lose the recursive automagic processing power of the tuple, but we can name each field literally. In C++, we can do both by means of an enum:</p>
  
  <pre><code>struct Message
  {
      enum FieldID { header, temperature, flag };
      static const char * FieldNames[] = { "header", "temperature", "flag" };
  
      typedef std::tuple&lt;int, double, char&gt; tuple_type;
  
      template &lt;FieldID I&gt;
      typename std::tuple_element&lt;I, tuple_type&gt;::type &amp; get()
      { return std::get&lt;I&gt;(data); }
  
      template &lt;FieldID I&gt;
      static const char * name() { return FieldNames[I]; }
  
      tuple_type data;
  };
  </code></pre>
  
  <p>Now I can say, <code>Message m; m.get&lt;Message::header&gt;() = 12;</code> etc., and I can recurse over the fields and make each print out their own value prefixed by their own name, etc.</p>
  
  <p><br></p>
  
  <p>Now the question: <em>How can I author such code efficiently, without repetition?</em></p>
  
  <p>Ideally, I want to be able to say this:</p>
  
  <pre><code>START_MESSAGE(Message)
  ADDFIELD(int, header)
  ADDFIELD(double, temperature)
  ADDFIELD(char, flag)
  END_MESSAGE
  </code></pre>
  
  <p>Is there any way, combining preprocessor, Boost and C++11, to achieve something like this without the need for external generation tools? (I think Boost.Preprocessor calls this "horizontal" and "vertical" repetition. I need to "transpose" the field data somehow.) The key feature here is that I never have to repeat any of the information, and that modifying or adding one field only requires one single change.</p>