title: User Defined C++11 enum class Default Constructor
body: <p>Is there a way to specify the default constructor of an <code>enum class</code>?</p>
  
  <p>I am using an <code>enum class</code> to specify a set of values which are allowable for a particular datatype in a library: in this case, it's the GPIO pin id numbers of a Raspberry Pi. It looks something like this:</p>
  
  <p><code>enum class PinID : int {N4 = 4, N17 = 17, /* ...etc... */ }</code></p>
  
  <p>The point of me doing this instead just of using, say, an <code>int</code> is to ensure that code is safe: I can <code>static_assert</code> (or otherwise compile-time ensure -- the actual method used is not important to me) things like that someone hasn't made a spelling error (passing a 5 instead of a 4, etc), and I get automatic error messages for type mismatches, etc.</p>
  
  <p>The problem then is that <code>enum class</code> has a default constructor that -- for compatibility's sake with C's <code>enum</code>s I assume (since they have the same behaviour) -- initializes to the <code>enum class</code> equivalent of <code>0</code>. In this case, there is no <code>0</code> value. This means that a user making a declaration/definition like:</p>
  
  <p><code>PinID pid = PinID();</code></p>
  
  <p>is getting an enumerator that isn't explicitly defined (and doesn't even seem to "exist" when one looks at the code), and can lead to runtime errors. This also means that techniques like <code>switch</code>ing over the values of explicitly defined enumerators is impossible without having an error/default case -- something I want to avoid, since it forces me to either <code>throw</code> or do something like return a <code>boost::optional</code>, which are less amenable to static analysis.</p>
  
  <p>I tried to define a default constructor to no avail. I (desperately) tried to define a function which shares the name of the <code>enum class</code>, but this (rather unsurprisingly) resulted in strange compiler errors. I want to retain the ability to cast the <code>enum class</code> to <code>int</code>, with all <code>N#</code> enumerators mapping to their respective <code>#</code>, so merely "defining", say, N4 = 0 is unacceptable; this is for simplicity and sanity.</p>
  
  <p>I guess my question is two-fold: is there a way to get the kind of static safety I'm after using <code>enum class</code>? If not, what other possibilities would one prefer? What I want is something which:</p>
  
  <ol>
  <li>is default constructable</li>
  <li>can be made to default construct to an arbitrary valid value</li>
  <li>provides the "finite set of specified" values afforded by <code>enum class</code>es</li>
  <li>is at least as type safe as an <code>enum class</code></li>
  <li>(preferably) doesn't involve runtime polymorphism</li>
  </ol>
  
  <p>The reason I want default constructability is because I plan to use <code>boost::lexical_cast</code> to reduce the syntactic overhead involved in conversions between the <code>enum class</code> values, and the actual associated <code>string</code>s which I output to the operating system (sysfs in this case); <code>boost::lexical_cast</code> requires default constructability.</p>
  
  <p>Errors in my reasoning are welcome -- I am beginning to suspect that <code>enum class</code>es are the right object for the wrong job, in this case; clarification will be offered if asked. Thank you for your time.</p>