title: Count on enum C++ automatic
body: <p>I've come to a pattern when writing enums in C++. It is like this:</p>
  
  <pre><code>class Player
  {
  public:
      class State
      {
      public:
          typedef enum
          {
              Stopped, 
              Playing, 
              Paused
          }PossibleValues;  
  
          static const int Count() {return Paused+1;};
          static const PossibleValues Default() {return Stopped;};
      };
  
      //...
  }
  </code></pre>
  
  <p>This solves a some of the usual issues with enums, like pollution of outside namespaces, etc. But there is still a thing I don't like: The Count() is done manually. There are only two ways I know how to do it: this one is calculated from Last+1; or write plain hardcoded.</p>
  
  <p>Question is: Is there some way, like using preprocessor macros, that automatically gets the count, to put it after in the Count() method? Attention: I don't want to have a last fake element called Count inside the enum, polluting it!</p>
  
  <p>Thanks in advance!</p>
  
  <p><strong>UPDATE 1:</strong></p>
  
  <p>There is an interesting discussion on <a href="https://groups.google.com/a/isocpp.org/forum/?fromgroups#!topic/reflection/hjSbj2_Rvpc" rel="nofollow">Implementation of N4428 enum reflection in standard C++11 (partial)</a> for a proposal of more advanced enums.</p>
  
  <p><strong>UPDATE 2:</strong></p>
  
  <p>Interesting document <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/n4451.pdf" rel="nofollow">N4451- Static reflection (rev. 3)</a> on its sections 3.16, 3.17, A.7, A.8 about MetaEnums and MetaEnumClasses.</p>
  
  <p><strong>UPDATE 3:</strong></p>
  
  <p>I came to another interesting pattern using an enum class, after I've seen <a href="https://bytes.com/topic/c/answers/127908-numeric_limits-specialization#post444962" rel="nofollow">https://bytes.com/topic/c/answers/127908-numeric_limits-specialization#post444962</a>. If the enum class's <strong>enumerator list is continuously integer</strong>, by defining its maximum and its minimum, we can check whether a value belongs to it or not. </p>
  
  <p>If the purpose of using the <code>Count()</code> method on the <code>Player::State</code> was to check if a value was in the enum, that purpose has also been achieved with the numeric_limits approach, and is even superior, as it is not required the enumerator list begins with a ZERO valued item!</p>
  
  <pre><code>enum class Drink
  {
      Water,
      Beer,
      Wine,
      Juice,
  };
  
  
  #pragma push_macro("min")
  #undef min
  
  #pragma push_macro("max")
  #undef max
  
  namespace std
  {
      template &lt;&gt; class numeric_limits &lt; Drink &gt;
      {
      public:
          static const/*expr*/ bool is_specialized = true;
  
          static const/*expr*/ Drink min() /*noexcept*/ { return Drink::Water; }
          static const/*expr*/ Drink max() /*noexcept*/ { return Drink::Juice; }
  
          static const/*expr*/ Drink lowest() /*noexcept*/ { return Drink::Water; }
  
          static const/*expr*/ Drink default() /*noexcept*/ { return Drink::Beer; }
      };
  }
  
  #pragma pop_macro("min")
  #pragma pop_macro("max")
  </code></pre>
  
  <p>CASES OF USE:</p>
  
  <p>A variable from the application:</p>
  
  <pre><code>Drink m_drink;
  </code></pre>
  
  <p>which in constructor is initialized with:</p>
  
  <pre><code>m_drink = numeric_limits&lt;Drink&gt;::default();
  </code></pre>
  
  <p>On the initialization of a form, I can do:</p>
  
  <pre><code>pComboDrink-&gt;SetCurSel(static_cast&lt;int&gt;(theApp.m_drink));
  </code></pre>
  
  <p>On it, for adapting the interface to changes done by the user, I can do a switch with scoped enum class values:</p>
  
  <pre><code>switch (static_cast&lt;Drink&gt;(pComboDrink-&gt;GetCurSel()))
  {
  case Drink::Water:
  case Drink::Juice:
      pAlcohoolDegreesControl-&gt;Hide();
  break;
  
  case Drink::Beer:
  case Drink::Wine:
      pAlcohoolDegreesControl-&gt;Show();
  break;
  
  default:
      break;
  }
  </code></pre>
  
  <p>And on the dialog's confirmation procedure (<code>OnOK</code>), I can check if the value is out of boundaries, before saving it to the respective app var:</p>
  
  <pre><code>int ix= pComboDrink-&gt;GetCurSel();
  
  if (ix == -1)
      return FALSE;
  
  #pragma push_macro("min")
  #undef min
  
  #pragma push_macro("max")
  #undef max
  
  if (ix &lt; static_cast&lt;int&gt; (std::numeric_limits&lt;Drink&gt;::min()) ||  ix &gt; static_cast&lt;int&gt; (std::numeric_limits&lt;Drink&gt;::max()) )
      return FALSE;
  
  #pragma pop_macro("min")
  #pragma pop_macro("max")
  
  theApp.m_drink= static_cast&lt;Drink&gt;(ix);
  </code></pre>
  
  <p>NOTES:</p>
  
  <ol>
  <li>The keywords <code>constexpr</code> (I commented <code>/*expr*/</code>, leaving it as <code>const</code> ) and <code>noexcept</code> are commented only because the compiler I am using (Visual C++ 2013) does not support them yet at the current version.</li>
  <li>Maybe you do not need the logic to temporary undefine the min and max macros.</li>
  <li>I know that the <code>default()</code> does not fit on a "numeric limits" scope; but it seemed an handy place to put it on; even it coincides with the <code>default</code> word that in some contexts is a keyword!</li>
  </ol>