title: Unexpected result for a type counter using templates with function local types in Clang
body: <p>I wrote a class template based on two types that is assigned a unique index based on its template parameters:</p>
  
  <pre><code>template&lt;typename SK,typename T&gt;
  struct Component {
      static uint const index;
  };
  </code></pre>
  
  <p>The expectation is that for each new type, <code>index</code> is incremented:</p>
  
  <pre><code>Component&lt;X,A&gt;::index; // 0
  Component&lt;X,B&gt;::index; // 1
  
  Component&lt;Y,A&gt;::index; // 0
  Component&lt;Y,B&gt;::index; // 1
  // ...etc
  </code></pre>
  
  <p>The complete code that assigns the indices is as follows:</p>
  
  <pre><code>using uint = unsigned int;
  
  template&lt;typename SK,typename T&gt;
  struct Component
  {
      static uint const index;
  };
  
  template&lt;typename SK&gt;
  class ComponentCount
  {
      template&lt;typename CSK,typename CT&gt;
      friend struct Component;
  
  private:
      template&lt;typename T&gt;
      static uint next() {
          return ComponentCount&lt;SK&gt;::get_counter();
      }
  
      static uint get_counter()
      {
          static uint counter = 0;
          return counter++;
      }
  };
  </code></pre>
  
  <p>This works as expected in GCC (5.1) and MSVC with the following test:</p>
  
  <pre><code>// global scope
  struct X {};
  struct Y {};
  
  int main()
  {
      // function scope
      struct Z{};
  
      uint x0 = Component&lt;X,int&gt;::index;
      uint x1 = Component&lt;X,double&gt;::index;
      uint x2 = Component&lt;X,double&gt;::index;
      uint x3 = Component&lt;X,std::string&gt;::index;
      uint x4 = Component&lt;X,int&gt;::index;
      uint x5 = Component&lt;X,int&gt;::index;
  
      std::cout &lt;&lt; x0 &lt;&lt; ", " &lt;&lt; x1 &lt;&lt; ", " &lt;&lt; x2 &lt;&lt; ", "
                &lt;&lt; x3 &lt;&lt; ", " &lt;&lt; x4 &lt;&lt; ", " &lt;&lt; x5 &lt;&lt; std::endl;
  
      uint y0 = Component&lt;Y,int&gt;::index;
      uint y1 = Component&lt;Y,double&gt;::index;
      uint y2 = Component&lt;Y,double&gt;::index;
      uint y3 = Component&lt;Y,std::string&gt;::index;
      uint y4 = Component&lt;Y,int&gt;::index;
      uint y5 = Component&lt;Y,int&gt;::index;
  
      std::cout &lt;&lt; y0 &lt;&lt; ", " &lt;&lt; y1 &lt;&lt; ", " &lt;&lt; y2 &lt;&lt; ", "
                &lt;&lt; y3 &lt;&lt; ", " &lt;&lt; y4 &lt;&lt; ", " &lt;&lt; y5 &lt;&lt; std::endl;
  
      uint z0 = Component&lt;Z,int&gt;::index;
      uint z1 = Component&lt;Z,double&gt;::index;
      uint z2 = Component&lt;Z,double&gt;::index;
      uint z3 = Component&lt;Z,std::string&gt;::index;
      uint z4 = Component&lt;Z,int&gt;::index;
      uint z5 = Component&lt;Z,int&gt;::index;
  
      std::cout &lt;&lt; z0 &lt;&lt; ", " &lt;&lt; z1 &lt;&lt; ", " &lt;&lt; z2 &lt;&lt; ", "
                &lt;&lt; z3 &lt;&lt; ", " &lt;&lt; z4 &lt;&lt; ", " &lt;&lt; z5 &lt;&lt; std::endl;
  
      return 0;
  }
  </code></pre>
  
  <p>The output is</p>
  
  <pre><code>0, 1, 1, 2, 0, 0
  0, 1, 1, 2, 0, 0
  0, 1, 1, 2, 0, 0
  </code></pre>
  
  <p>However with Clang (3.6.1), the output differs:</p>
  
  <pre><code>0, 1, 1, 2, 0, 0
  0, 1, 1, 2, 0, 0
  5, 2, 2, 3, 5, 5
  </code></pre>
  
  <p>Specifically, the indices generated for function local types (ie. 'Z') do something strange. Its like they increment and reassign the index every time <code>Component&lt;Z,...&gt;</code> is called.</p>
  
  <p>Why does this happen? Is it a compiler bug? Are there any special considerations when using function local types with templates (post C++11)?</p>
  
  <p>A complete example can be found here:
  <a href="http://coliru.stacked-crooked.com/a/7fcb989ae6eab476" rel="nofollow noreferrer">http://coliru.stacked-crooked.com/a/7fcb989ae6eab476</a></p>
  
  <p>== Edit ==</p>
  
  <p>I decided to post the issue to clang's bugtracker, so if anyone else runs into this:</p>
  
  <p><a href="https://llvm.org/bugs/show_bug.cgi?id=24048" rel="nofollow noreferrer">https://llvm.org/bugs/show_bug.cgi?id=24048</a></p>