title: Underlying type of a C++ enum in C++0x
body: <p>I've been trying to read a bit of the C++ standard to figure out how enum's work. There's actually more there than I originally thought.</p>
  
  <p>For a scoped enumeration, it's clear that the underlying type is <code>int</code> unless otherwise specified with an enum-base clause (it can be any integral type).</p>
  
  <pre><code>enum class color { red, green, blue};  // these are int
  </code></pre>
  
  <p>For unscoped enumerations, it seems like the underlying type can be any integral type that will work and that it won't be bigger than an int, unless it needs to be.</p>
  
  <pre><code>enum color { red, green, blue};  // underlying type may vary
  </code></pre>
  
  <p>Since the underlying type of unscoped enumarations are not standardized, what's the best way of dealing with serializing instances of one? So far, I've been converting to <code>int</code> when writing then serializing into an <code>int</code> and setting my <code>enum</code> variable in a switch when reading, but it seems a bit clunky. Is there a better way?</p>
  
  <pre><code>enum color { red, green, blue };
  color c = red;
  // to serialize
  archive &lt;&lt; (int)c;
  // to deserialize
  int i;
  archive &gt;&gt; i;
  switch(i) {
    case 0: c = red; break;
    case 1: c = green; break;
    case 2: c = blue; break;
  }
  </code></pre>