Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I don't really know why you wish to add up matrices of different dimensions, but in order to do it, you must turn the method into a method template on the dimension of the matrix argument:</p> <pre><code>#include &lt;type_traits&gt; template &lt;int Roz2&gt; Matrix&amp; operator+(Matrix&lt;T,Roz2&gt; b) { // check matrix argument size compatibility std::static_assert(Roz2 &gt;= Roz, "incompatible matrices for operator+()"); Matrix&lt;T,Roz&gt; tmp; for(int i=0;i&lt;Roz;++i) for(int j=0;j&lt;Roz;++j) tmp.tab[i][j]=this-&gt;tab[i][j]+b.tab[i][j]; } </code></pre> <p>Since <code>Matrix&lt;T,0&gt;</code> is a specialization to handle dynamic dimension, you must specialize the operator for it:</p> <pre><code>// in the generic template Matrix&amp; operator+(Matrix&lt;T,0&gt; b) { assert (b.z &lt; Roz, "incompatible dynamic matrix for operator+"); Matrix&lt;T,0&gt; tmp(b.Roz);; for(int i=0;i&lt;Roz;++i) for(int j=0;j&lt;Roz;++j) tmp.tab[i][j]=this-&gt;tab[i][j]+b.tab[i][j]; } </code></pre> <p>and in <code>Matrix&lt;T,0&gt;</code>:</p> <pre><code>template &lt;int Roz&gt; Matrix&amp; operator+(Matrix&lt;T,Roz&gt; b) { assert(Roz &gt;= z,"...."); Matrix&lt;T,0&gt; tmp(b.z); for(int i=0;i&lt;z;++i) for(int j=0;j&lt;z;++j) tmp.tab[i][j]=this-&gt;tab[i][j]+b.tab[i][j]; } </code></pre> <p>In the above solution, however, the operator is not commutative. In order to make it so, we need to do some change:</p> <pre><code> // define a min operator as constexpr in case the existing one is not template&lt;typename T&gt; constexpr T const&amp; rmin(T const&amp; a, T const&amp; b) { return a &gt; b ? b : a; } // generic operator template &lt;int Roz2&gt; Matrix&lt;T,rmin(Roz,Roz2)&gt;&amp; operator+(Matrix&lt;T,Roz2&gt; b) { constexpr int R = min(Roz,Roz2); Matrix&lt;T,R&gt; tmp; for(int i=0;i&lt;R;;++i) for(int j=0;j&lt;R;++j) tmp.tab[i][j]=this-&gt;tab[i][j]+b.tab[i][j]; } // specialized operator on Roz=0 argument Matrix&lt;T,0&gt;&amp; operator+(Matrix&lt;T,0&gt; b) { const int r = min(z,b.z); Matrix&lt;T,0&gt; tmp(r); for(int i=0;i&lt;r;;++i) for(int j=0;j&lt;r;++j) tmp.tab[i][j]=this-&gt;tab[i][j]+b.tab[i][j]; } // specialized operator in Roz=0 template Matrix&amp; operator+(Matrix&lt;T,Roz&gt; b) { return b + *this; } </code></pre> <p>You will have to duplicate the code for <code>const &amp;</code> parameters taking operators (or perhaps simply get rid of the non const parameter versions which don't seem very useful in that context).</p> <p>Note the use of a <a href="http://en.cppreference.com/w/cpp/language/static_assert" rel="nofollow noreferrer">static assert</a> to limit the use of the operator with matrices of equal or larger dimension (this is a C++11 feature) in the non commutative version of the operators.</p> <p>See <a href="https://stackoverflow.com/questions/5605142/stdmax-and-stdmin-not-constexpr">this question</a> on the topic of <code>min</code> not being a <code>constexpr</code> function template for further details.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload