title: Default value for Template Class, C++
body: <p>Is there a way to give a default value for a template class?</p>
  
  <p>E.g. Say I have a class <code>TimeSeries</code>, that is templated.  And I have a function called <code>Foo()</code> that returns <code>T</code>.</p>
  
  <pre><code>template &lt;typename T&gt;
  class TimeSeries
  {
      T foo();
  }
  </code></pre>
  
  <p>I want <code>Foo()</code> to do some calculation and return something of type <code>T</code>. But in case it can't, I want it to give a default value. If <code>T</code> was <code>double</code>, I want that to be <code>NaN</code>. If <code>T</code> was <code>int</code>, I want that to be <code>0</code>. </p>
  
  <pre><code>TimeSeries&lt;double&gt; t;
  t.foo(); // returns NAN
  
  TimeSeries&lt;int&gt; t;
  t.foo(); // returns 0
  </code></pre>
  
  <p>How would I achieve that? </p>
  
  <p>A bad solution is for <code>Foo()</code> to take in a default value. </p>
  
  <pre><code>template &lt;typename T&gt;
  class TimeSeries
  {
      T foo(T defaultValue)
      {
         T ret;
         // if OK
         ret = computeValue();
         // if not OK
         ret = defaultValue;
         return ret;
      }
  }
  </code></pre>
  
  <p>So default value would be <code>0</code>, or <code>NaN</code>, depending on the user.     </p>
  
  <p>Or am I asking for a flawed design in the first place? </p>
  
  <p>EDIT - 
  A logical question would be to ask what happens if <code>T</code> is not <code>int</code> or <code>double</code>. What does a default value even mean?</p>
  
  <p>The way I'm using <code>TimeSeries</code>, <code>T</code> is not exactly a general purpose type. It could only be 1 of <code>int</code>, <code>double</code>, and <code>string</code>. 
  I thought about simply writing 3 separate <code>TimeSeries</code> classes, but that seems like a flawed approach. </p>
  
  <p>I'm open to other ideas. </p>