Vahagn
Vahagn

Reputation: 426

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?

Upvotes: 0

Views: 341

Answers (2)

max66
max66

Reputation: 66200

Variant of the method 3 from MathanOliver: define operator+() (off topic suggestion: ever define operator+=() as method and operator+() as external function) not as method but as external function (you can make it friend to Matrix if needed).

template <typename T1, typename T2,
          typename Tr = decltype(std::declval<T1>() + std::declval<T2>())>
Matrix<Tr> operator+ (Matrix<T1> const & m1, Matrix<T2> const & m2)
 { 
   // something useful
   return {m1.width(), m1.height()};
 }

The following is a full compiling example

#include <cstring>
#include <utility>

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

      Matrix(const Matrix<T> & other)
         : Matrix(other.width(), other.height() )
       { std::memcpy(data_ptr_, other.data(), width_ * height_ * sizeof(T)); }
      virtual ~Matrix()
       { delete []data_ptr_; }
      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_;
 };

template <typename T1, typename T2,
          typename Tr = decltype(std::declval<T1>() + std::declval<T2>())>
Matrix<Tr> operator+ (Matrix<T1> const & m1, Matrix<T2> const & m2)
 {
   return {m1.width(), m1.height()};
 }


int main ()
 {
   Matrix<int>   m1{1, 2};
   Matrix<float> m2{1, 2};

   auto m3 = m1 + m2;
   auto m4 = m2 + m1;

   static_assert( std::is_same<decltype(m3), Matrix<float>>{}, "!" );
   static_assert( std::is_same<decltype(m4), Matrix<float>>{}, "!" );

   return 0;
 }

Obtaining the type of the returned matrix as default template value (Tr) you can explicit a different type, if you want, as follows

auto m5 = operator+<int, float, int>(m1, m2);

static_assert( std::is_same<decltype(m5), Matrix<int>>{}, "!" );

Upvotes: 1

NathanOliver
NathanOliver

Reputation: 180424

You can use method 3 and use C++11's auto return type deduction to figure out the type for you. Using

template<typename K>
auto operator+(const Matrix<K> &other) const -> Matrix<decltype(std::declval<T>() + std::declval<K>())>;

This says that the matrix returned will have the type of whatever a T added to a K would be.

You won't be able to make custom rules with this but it will follow the standard promotion/conversion rules.

Upvotes: 3

Related Questions