title: can you give a derived class object as a function parameter in place of a base class object
body: <p>I am trying to do matrix addition using expression templates and for this task I have base class: Exp</p>
  
  <pre><code>template&lt;typename subtype&gt;
  class Exp{
  public:
    inline const subtype&amp; self(void) const{
      return *static_cast&lt;const subtype*&gt;(this);
    }
  
  };
  </code></pre>
  
  <p>a derived class: matrix</p>
  
  <pre><code>template&lt;typename T,unsigned rows_,unsigned cols_ &gt;
  class matrix : public Exp&lt;matrix&lt;T,rows_,cols_&gt;&gt;{
     //some members
  };
  </code></pre>
  
  <p>and another derived class: addExp</p>
  
  <pre><code>template&lt;typename T, typename Op1 , typename Op2&gt;
  class addExp: public Exp&lt; addExp&lt;T,Op1,Op2&gt; &gt;{
    const Op1&amp; op1;
    const Op2&amp; op2;
  
  public:
    addExp(const Op1&amp; a, const Op2&amp; b): op1(a), op2(b){}
  
    T&amp; operator()(const std::size_t i,const std::size_t j) const{ 
      return op1(i,j) + op2(i,j); 
    }
  }; 
  </code></pre>
  
  <p>I am now trying to do operator overloading on addExp for adding matrices.</p>
  
  <pre><code>template&lt;typename T,typename Lhs, typename Rhs&gt;
  inline addExp&lt;T,Lhs, Rhs&gt;
  operator+(const Exp&lt;Lhs&gt; &amp;lhs, const Exp&lt;Rhs&gt; &amp;rhs) {
    return addExp&lt;T,Lhs, Rhs&gt;(lhs.self(), rhs.self());
  }
  </code></pre>
  
  <p>later in my code I try to put two matrix objects(which should have Exp as base class) as function parameters here but I get an error:</p>
  
  <pre><code>prog.cpp: In function ‘int main()’:
  prog.cpp:76:25: error: no match for ‘operator+’ (operand types are ‘matrix&lt;int, 3u, 3u&gt;’ and ‘matrix&lt;int, 3u, 3u&gt;’)
    matrix&lt;int,3,3&gt; m3 = m1+m2;
                         ~~^~~
  prog.cpp:69:1: note: candidate: template&lt;class T, class Lhs, class Rhs&gt; addExp&lt;T, Lhs, Rhs&gt; operator+(const Exp&lt;Lhs&gt;&amp;, const Exp&lt;Rhs&gt;&amp;)
   operator+(const Exp&lt;Lhs&gt; &amp;lhs, const Exp&lt;Rhs&gt; &amp;rhs) {
   ^~~~~~~~
  prog.cpp:69:1: note:   template argument deduction/substitution failed:
  prog.cpp:76:26: note:   couldn't deduce template parameter ‘T’
    matrix&lt;int,3,3&gt; m3 = m1+m2;  
  </code></pre>
  
  <p>where did I go wrong here and how do I fix this?</p>