title: Automating the generation of an enum lookup table
body: <p>I'm using a technique similar to <a href="http://www.itshouldjustworktm.com/?p=32" rel="nofollow">this</a> to associated information and/or actions with enumerators. It works pretty well, but there's a minor issue of needing to effectively list every constant twice, once in the header file in the enum declaration, and once in the source file in the lookup table. I'm wondering if there's any good way, probably with preprocessor trickery (Boost.Preprocessor is acceptable), to "automate" it so that I can enter the enum constants and associated values etc in just one place and have all the necessary stuff (perhaps even the lookup struct itself) get generated.</p>
  
  <p>I'd prefer a method that maintains vaguely enum-style syntax, if possible; eg, something like <code>DECLARE_ENUM(...) {E_CONST(...), E_CONST(...)};</code>. I've seen some sites mentioning the idea of double-including the header file in order to achieve this, i.e. something  like this:</p>
  
  <pre class="lang-cpp prettyprint-override"><code>#include "my_enum.hpp"
  #undef ENUM_HPP // undefine the include guard
  #undef E_CONST
  #define E_CONST(...) /* something here */
  #include "my_enum.hpp"
  </code></pre>
  
  <p>...but I'm not sure how useful that technique would be here. In particular, there is more than just the enum defined in the header; the lookup table struct is also there, along with a couple of other related enums and supporting functions.</p>
  
  <p>I'm already using a macro to define the elements in the lookup table (it uses C99 initializers so that the entries will always be in the right place even if I rearrange the order of the enum constants).</p>
  
  <p>A solution that can be applied to multiple enums would be nice as well.</p>
  
  <p>I'm using clang (Apple-3.1) for this and am not particularly worried about portability.</p>
  
  <p>I had an attempt somewhere that I threw away... I can't remember why it didn't work. Maybe I can find it in Time Machine...</p>