title: C++11 templates, determining return type
body: <p>I am building a matrix library and I am trying to use the <a href="http://en.wikipedia.org/wiki/Policy-based_design" rel="nofollow" title="policy-based design">policy-based design</a>.
  So my base classes are classes that provide a storage method and some
  access functions.
  I also have a function matrix which provides the mathematical functions.
  This works great, but there is a major problem with the operator*
  because of the return type. I will explain it with some code.</p>
  
  <p>Base class that provides a stack storage :</p>
  
  <pre><code>template &lt; typename T, unsigned int rows, unsigned int cols&gt;
  class denseStackMatrix {
  public:
      typedef T value_type;
  
  private:
      value_type grid[rows][cols];
      const unsigned int rowSize;
      const unsigned int colSize;
  </code></pre>
  
  <p>Then I have my matrix class which provides mathematical functionality :</p>
  
  <pre><code>template &lt;typename MatrixContainer &gt;
  class matrix : public MatrixContainer {
  public:
      typedef MatrixContainer Mcontainer;
  
      matrix&lt;Mcontainer&gt;&amp; operator +(const matrix&lt;Mcontainer&gt;&amp;);
      matrix&lt;Mcontainer&gt;&amp; operator *(const matrix&lt;Mcontainer&gt;&amp;);
  </code></pre>
  
  <p><code>operator+</code> always works, <code>operator*</code> only works for square matrix.
  So we still need one for all matrices. And that's were it goes
  wrong. I have already tried few things, but nothings works.
  I look for something like this, with the help of c++0x (usage of
  c++0x is not a requirement) 
  you shall notice the "???" :)</p>
  
  <pre><code>friend auto operator * (const matrix&lt;T1&gt;&amp; matrix1, const matrix&lt;T2&gt;&amp; matrix2)
  -&gt; decltype(matrix&lt;???&gt;);
  </code></pre>
  
  <p>An example of the problem</p>
  
  <pre><code>matrix&lt;denseStackMatrix&lt;int,3,2&gt; &gt; matrix1;
  matrix&lt;denseStackMatrix&lt;int,2,4&gt; &gt; matrix2;
  matrix&lt;denseStackMatrix&lt;int,3,4&gt; &gt; matrix3 = matrix1 * matrix2;
  </code></pre>
  
  <p>Here it will complain about the type, because it does not match any of the two parameter types. But the compiler needs to know the type at compile-time and I do not know how to provide it.</p>
  
  <p>I know there are other options for the design, but I am really looking for a solution for this scenario..</p>
  
  <p>Thank you !</p>