title: partial template specialization for dynamic dispatch
body: <p>I am attempting to write a dynamic dispatcher for a function that's templated on integer values (not on types).  While I could either write a code generator or use a big macro chain to create the dispatcher source, it seems that a templated solution would be more elegant.</p>
  
  <p>I've stripped down my dispatcher to a simple form (which doesn't actually do any dispatching):</p>
  
  <pre><code>// works fine with full template specialization
  template &lt;int N&gt;
  struct TestDispatcher1D {
    int f(int n) {
      if (n == N) return n; // replace me with actual dispatch
      TestDispatcher1D&lt;N-1&gt; t;
      return t.f(n);
    }
  };
  
  template&lt;&gt;
  struct TestDispatcher1D&lt;-1&gt; {
    int f(int n) { return -1; }
  };
  
  // partial template specialization is problematic
  template &lt;int M, int N&gt;
  struct TestDispatcher2D {
    int f(int m, int n);
  };
  
  template&lt;int M&gt;
  struct TestDispatcher2D&lt;M,-1&gt; {
    int f(int m, int n) { return -1; }
  };
  
  template&lt;int N&gt;
  struct TestDispatcher2D&lt;-1,N&gt; {
    int f(int m, int n) { return -1; }
  };
  
  template&lt;&gt;
  struct TestDispatcher2D&lt;-1,-1&gt; {
    int f(int m, int n) { return -1; }
  };
  
  template &lt;int M, int N&gt;
  int TestDispatcher2D&lt;M,N&gt;::f(int m, int n) {
    if ((n == N) &amp;&amp; (m == M)) return n + m; // replace me with actual dispatch
    if (m &lt; M) {
      if (n &lt; N) {
        TestDispatcher2D&lt;M-1,N-1&gt; t;
        return t(m,n);
      } else {
        TestDispatcher2D&lt;M-1,N&gt; t;
        return t(m,n);
      }
    } else {
      TestDispatcher2D&lt;M,N-1&gt; t;
      return t(m,n);
    }
  }
  
  // test code
  void testIt() {
    { 
      TestDispatcher1D&lt;16&gt; t; 
      t.f(16); 
    }
    {
      TestDispatcher1D&lt;16&gt;t; 
      t.f(0);
    }
    {
      TestDispatcher2D&lt;16,16&gt;t; 
      t.f(8,8);
    }
  }
  </code></pre>
  
  <p>When compiling this on gcc 4.1.1, I get the following errors:</p>
  
  <pre>
  t.cpp: In member function 'int TestDispatcher2D::f(int, int) [with int M = 16, int N = 16]':
  t.cpp:63:   instantiated from here
  t.cpp:40: error: no match for call to '(TestDispatcher2D) (int&, int&)'
  t.cpp:63:   instantiated from here
  t.cpp:43: error: no match for call to '(TestDispatcher2D) (int&, int&)'
  t.cpp:63:   instantiated from here
  t.cpp:47: error: no match for call to '(TestDispatcher2D) (int&, int&)'
  </pre>
  
  <p>Apparently, when I try to create the recursive objects, the compiler isn't treating this as a request to instantiate a new template.</p>
  
  <p>Any suggestions?</p>