lundi 4 juin 2018

Implicit conversion class templates

I am trying to implement a Matrix class with linear algebraic operations. I want to make the class available for a few value types like uint, uchar, float, double.

The header looks as follows:

template<typename T>
class Matrix{
public:
    Matrix(int width, int height);
    Matrix(const Matrix<T> & other);
    virtual ~Matrix();
    unsigned int width() const { return width_; }
    unsigned int height() const { return height_; };
    T * data() const { return data_ptr_; };
private:
  T * data_ptr_;
  unsigned int width_;
  unsigned int height_;
}

The source file looks as follows.

template<typename T>
Matrix<T>::Matrix(int width, int height ): width_(width), height_(height)
{
  data_ptr_ = new T[width * height];
}

template<typename T>
Matrix<T>::Matrix(const Matrix<T> & other): Matrix(other.width(), other.height() )
{
   memcpy(data_ptr_, other.data(), width_ * height_ * sizeof(T);
}

template<typename T>
Matrix<T>::~Matrix()
{
   delete []data_ptr_;
}

template class Matrix<double>;
template class Matrix<float>;
...

Now I want to define an operator "+" which will return a Matrix of the type which an ordinary c++ conversion does when adding two values, i.e.

Matrix<double> + Matrix<float> => Matrix<double>
Matrix<int> + Matrix<float> => Matrix<float>

and I want to be able to do this without explicit conversions. For example

Matrix<float> float_matrix(10,20);
Matrix<int> int_matrix(10,20);

auto sum_matrix = float_matrix + int_matrix;

so the sum should have type float.

I tried 2 methods with no success, however.

Method 1 Define operator "+" as

//inside class definition
Matrix<T> operator+(const Matrix<T> &other) const;

and define implicit constructors like

//inside the class declaration
template<typename K>
Matrix(const Matrix<K> & other);

and instantiate them only in the obvious hierarchical order: uchar-> uint->float->double, however I still have to manually cast operands of different types.

Method 2

Define operator "+" as

//inside the class declaration
template<typename K, typename R>
Matrix<R> operator+(const Matrix<K> &other) const;

and write specializations for each case, however, I could not get the compiler to correctly deduce the template arguments.

Neither of methods seem to be correct.

Can anyone point me a direction ?

Aucun commentaire:

Enregistrer un commentaire