title: Undefined reference to specialized template member
body: <p>I have a class parameterized by a template template class with a static member function:</p>
  
  <pre><code>template &lt;template &lt;typename&gt; class F&gt;
  struct A {
    static int foo();
  };
  </code></pre>
  
  <p>This class has no default definition for <code>foo</code>, and must be specialized for different types.</p>
  
  <p>I also have another class parameterized by a template template class with a nested template class:</p>
  
  <pre><code>template &lt;template &lt;typename&gt; class F&gt;
  struct B {
    template &lt;typename T&gt;
    struct C {};
  };
  </code></pre>
  
  <p>I want <code>C</code> to specialize <code>A</code> for any template template class <code>F</code> that specializes <code>A</code> already:</p>
  
  <pre><code>template &lt;template &lt;typename&gt; class F&gt;
  struct A&lt;B&lt;F&gt;::template C&gt; {
    static int foo();
  };
  
  template &lt;template &lt;typename&gt; class F&gt;
  int A&lt;B&lt;F&gt;::template C&gt;::foo() {
    return A&lt;F&gt;::foo() / 2;
  }
  </code></pre>
  
  <p>So, if I have a class that specializes <code>A</code>:</p>
  
  <pre><code>template &lt;typename T&gt;
  struct E {};
  
  template &lt;&gt;
  int A&lt;E&gt;::foo() {
    return 42;
  }
  </code></pre>
  
  <p>I would expect to be able to use the specialization like this (and return 21):</p>
  
  <pre><code>int bar() {
    return A&lt;B&lt;E&gt;::template C&gt;::foo();
  }
  </code></pre>
  
  <p>However, this fails to link - it cannot find the reference to <code>A&lt;B&lt;E&gt;::C&gt;::foo()</code>.</p>
  
  <p>(Note that all of this is in a single file - there is nothing weird happening with headers here)</p>
  
  <p>It appears that the compiler is trying to use the primary template for <code>A</code> rather than the specialization, which means that <code>foo</code> is undefined. Why does it not use the specialization in this instance?</p>
  
  <h2>Full Example</h2>
  
  <pre><code>template &lt;template &lt;typename&gt; class F&gt;
  struct A {
    static int foo();
  };
  
  template &lt;template &lt;typename&gt; class F&gt;
  struct B {
    template &lt;typename T&gt;
    struct C {};
  };
  
  template &lt;template &lt;typename&gt; class F&gt;
  struct A&lt;B&lt;F&gt;::template C&gt; {
    static int foo();
  };
  
  template &lt;template &lt;typename&gt; class F&gt;
  int A&lt;B&lt;F&gt;::template C&gt;::foo() {
    return A&lt;F&gt;::foo() / 2;
  }
  
  template &lt;typename T&gt;
  struct E {};
  
  template &lt;&gt;
  int A&lt;E&gt;::foo() {
    return 42;
  }
  
  int bar() {
    // Link fails - error: undefined reference to 'A&lt;B&lt;E&gt;::C&gt;::foo()'
    return A&lt;B&lt;E&gt;::template C&gt;::foo();
  }
  </code></pre>