title: Does C++ support compile-time counters?
body: <p>For the purpose of introspection, sometimes I've wanted to automatically assign serial numbers to types, or something similar.</p>
  <p>Unfortunately, template metaprogramming is essentially a functional language, and as such lacks global variables or modifiable state which would implement such a counter.</p>
  <p>Or does it?</p>
  <hr />
  <p>Example code by request:</p>
  <pre><code>#include &lt;iostream&gt;
  
  int const a = counter_read;
  counter_inc;
  counter_inc;
  counter_inc;
  counter_inc;
  counter_inc;
  
  int const b = counter_read;
  
  int main() {
      std::cout &lt;&lt; a &lt;&lt; ' ' &lt;&lt; b &lt;&lt; '\n'; // print &quot;0 5&quot;
      
      counter_inc_t();
      counter_inc_t();
      counter_inc_t();
      
      std::cout &lt;&lt; counter_read &lt;&lt; '\n'; // print &quot;8&quot;
      
      struct {
          counter_inc_t d1;
          char x[ counter_read ];
          counter_inc_t d2;
          char y[ counter_read ];
      } ls;
      
      std::cout &lt;&lt; sizeof ls.x &lt;&lt; ' ' &lt;&lt; sizeof ls.y &lt;&lt; '\n'; // print &quot;9 10&quot;
  }
  </code></pre>