While implementing method and operator overloading in some of my classes to leverage the rvalue references in C++, often I write some code with poor design violating the DRY Principle. What would be a better alternative for the code snippet below? (This code is just to illustrate the problem)
class matrix_2_2
{
int _m[2][2];
public:
matrix_2_2 operator*(const matrix_2_2& m) const &
{
matrix_2_2 res;
for(int i = 0 ; i < 2 ; i ++)
for(int j = 0 ; j < 2 ; j++)
for(int k = 0 ; k < 2 ; k++)
res._m[i][j] = (res._m[i][j] + _m[i][k]*m._m[k][j]);
return res;
}
matrix_2_2 operator*(matrix_2_2&& m) &&
{
matrix_2_2 res;
for(int i = 0 ; i < 2 ; i ++)
for(int j = 0 ; j < 2 ; j++)
for(int k = 0 ; k < 2 ; k++)
res._m[i][j] = (res._m[i][j] + _m[i][k]*m._m[k][j]);
return move(res);
}
This code presents a lot of repetition in the implementations details, I would like to encapsulate the logic and reuse it in different overloads, without losing the movable advantage because of rvalue to lvalue implicity conversions.
Aucun commentaire:
Enregistrer un commentaire