title: Recursive template definition
body: <p>I have a recursive template definition (I just made up that term). I think code explains it better.</p>
  
  <pre><code>template&lt;typename X&gt;
  class Domain
  {
      public:
          X begin;
          X end;
  
          Domain(
              X _begin, 
              X _end)
              : begin(_begin)
              , end(_end)
          {
              // ...
          }
  
          bool Contains(
             const X&amp; t) const
          {
              // ...
          }
  };
  
  template&lt;typename X, typename Y&gt;
  class IFunction
  {
      public:
          Domain&lt;X&gt; myDomain;
  
      public:
          IFunction(
              const Domain&lt;X&gt;&amp; dom)
              : myDomain(dom)
          {
  
          }
  
          virtual Y
          Calc(
              const X&amp; IV) const = 0;
  
          virtual IFunction&lt;X, Y&gt;*
          GetDerivative() const = 0;
  };
  
  template&lt;typename X, typename Y, int n&gt;
  class NthOrderFunction
      : public IFunction&lt;X, Y&gt;
  {
      public:
          double coeffs[n+1];
  
      public:
          NthOrderFunction(
              const Domain&lt;X&gt;&amp; dom,
              ... )
              : IFunction(dom)
          {
  
          }
  
          virtual Y
          Calc(
              const X&amp; IV) const
          {
              // temporary compile solution
              return Y();
          }
  
          virtual IFunction&lt;X, Y&gt;*
          GetDerivative() const
          {
              if ( n &gt; 1 )
              {
                  return new NthOrderFunction&lt;X, Y, n-1&gt;(dom, ...);
              }
              return new FlatLine&lt;X, Y&gt;(dom);
          }
  };
  </code></pre>
  
  <p>I took out a lot of inheritance and other relations to keep it readable, simple and mysterious. So a new typo might have snuck in while editing the code, but please ignore it. The code has worked fine for years, the only problem I have is the one I'm going to point out.</p>
  
  <p>I recently added a "GetDerivative" function, and it's implementation in the NthOrderFunction class is giving me problems. I know that template classes are defined before compilation but after the preprocessing. As such, I can't see how to bring this functionality ever to work. Every NthOrderFunction with template parameter n, requires a NthOrderFunction with template parameter n-1. You can see that this is a problem. The thing is, even though n will never be negative in use, no amount of coding I do, will convince the "template definition engine" to not bother with instances of n &lt; 1;</p>
  
  <p>has anyone ever had any problems with this? And what solutions did you come up with?</p>