title: Understanding (simple?) C++ Partial Template Specialization
body: <p><strong>Note:</strong> this seems to be a repost of a problem: <a href="https://stackoverflow.com/questions/5206080/c-overload-templated-class-method-with-a-partial-specilization-of-that-method/5206117#5206117">C++ - Overload templated class method with a partial specilization of that method</a></p>
  
  <p>I have boiled down a problem I am having with C++ template specialization down to a simple case.</p>
  
  <p>It consists of a simple 2-parameter template class <code>Thing</code>, where I would like to specialize <code>Thing&lt;A,B&gt;::doSomething()</code> for <code>B=int</code>.</p>
  
  <pre><code>#include &lt;cstdio&gt;
  
  // A 3-parameter template class.
  template &lt;class A, class B&gt;
  class Thing
  {
  public:
      Thing(A a, B b) : a_(a), b_(b) {}
      B doSomething();
  private:
      A a_;
      B b_;
  };
  
  // The generic case works as expected.
  template &lt;class A, class B&gt;
  B Thing&lt;A,B&gt;::doSomething()
  {
      return b_;
  }
  
  // This specialization does not work!
  template &lt;class A&gt;
  int Thing&lt;A,int&gt;::doSomething()
  {
      return b_+1;
  }
  
  int main() {
      // Setup our thing.
      Thing&lt;double,int&gt; thing(1.0,2);
      // This doesn't compile - but works with the generic case.
      printf("Expecting 3, and getting %i\n", thing.doSomething());
      // Clean up.
      return 0;
  }
  </code></pre>
  
  <p>Unfortunately, <code>g++</code> exits with the error:</p>
  
  <pre><code>partial_specialization.cpp:30: error: invalid use of incomplete type ‘class Thing&lt;A, int&gt;’
  partial_specialization.cpp:8: error: declaration of ‘class Thing&lt;A, int&gt;’
  </code></pre>
  
  <p>The <code>clang++</code> compiler is a bit more verbose, but has the same problem:</p>
  
  <pre><code>partial_specialization.cpp:30:19: error: nested name specifier 'Thing&lt;A, int&gt;::' for declaration does not
        refer into a class, class template or class template partial specialization
  int Thing&lt;A,int&gt;::doSomething()
      ~~~~~~~~~~~~~~^
  partial_specialization.cpp:32:12: error: use of undeclared identifier 'b_'
      return b_+1;
             ^
  2 errors generated.
  </code></pre>
  
  <p>I have read and understood that partial template specializations on functions aren't allowed - but I thought I was partially specializing over classes of <code>Thing</code> in this case.</p>
  
  <p>Any ideas?</p>
  
  <p><strong>What I did:</strong> A workaround, as determined from the link provided by the accepted answer:</p>
  
  <pre><code>template&lt; class T &gt;
  inline T foo( T const &amp; v ) { return v; }
  
  template&lt;&gt;
  inline int foo( int const &amp; v ) { return v+1; }
  
  // The generic case works as expected.
  template &lt;class A, class B&gt;
  B Thing&lt;A,B&gt;::doSomething()
  {
      return foo(b_);
  }
  </code></pre>