title: How to implement optional template parameters?
body: <p>How do I implement optional template parameters? </p>
  
  <p>I'd like to have a class <code>MyStruct&lt;T1,T2,T3&gt;</code>, where it's allowed to use only the first or the first two params. Now, the functions that process <code>MyStruct&lt;T1,T2,T3&gt;</code> should also somehow deal correctly with the unused template params. </p>
  
  <p>Example:</p>
  
  <pre><code>#include &lt;iostream&gt;
  
  template&lt;class T1, class T2, class T3&gt;
  struct MyStruct {
    T1 t1; T2 t2; T3 t3;
    MyStruct() {}
    MyStruct(T1 const&amp; t1_, T2 const&amp; t2_, T3 const&amp; t3_)
      : t1(t1_), t2(t2_), t3(t3_) {}
  };
  
  template&lt;class T1, class T2, class T3&gt;
  MyStruct&lt;T1, T2, T3&gt; myplus(MyStruct&lt;T1, T2, T3&gt; const&amp; x,
                  MyStruct&lt;T1, T2, T3&gt; const&amp; y) {
    return MyStruct&lt;T1, T2, T3&gt;(x.t1 + y.t1, x.t2 + y.t2, x.t3 + y.t3);
  }
  
  int main() {
    typedef MyStruct&lt;int, double, std::string&gt; Struct;
    Struct x(2, 5.6, "bar");
    Struct y(6, 4.1, "foo");
    Struct result = myplus(x, y);
    // (8, 9.7, "barfoo")
    std::cout &lt;&lt; result.t1 &lt;&lt; "," &lt;&lt; result.t2 &lt;&lt; "," &lt;&lt; result.t3;
  }
  </code></pre>
  
  <p>Now I'd like to change the code so that the above <code>main()</code> function still works, but the following would also work:</p>
  
  <pre><code>typedef MyStruct&lt;std::string, int&gt; Struct;
  // result: ("barfoo", 5)
  Struct result = myplus(Struct("bar", 2), Struct("foo", 3));
  </code></pre>
  
  <p>Or this:</p>
  
  <pre><code>typedef MyStruct&lt;int&gt; Struct;
  // result: (5)
  Struct result = myplus(Struct(2), Struct(3));
  </code></pre>
  
  <p>I think <code>boost::tuple</code> uses a similar trick, where you may use <code>boost::tuple&lt;A&gt;</code>, <code>boost::tuple&lt;A,B&gt;</code>, <code>boost::tuple&lt;A,B,C&gt;</code>, but I'm not sure how they do it. </p>