0

Im trying to write some simple math libary in C++ using Expression templates.

template <typename E1, typename E2>
class MatSum : public MatExpression<MatSum<E1, E2>> {
E1 const& _u;
E2 const& _v;
public:
MatSum(E1 const& u, E2 const& v) :_u(u), _v(v) {
    assert(u.size() == v.size());
}

GLfloat operator[](size_t i) const { return _u[i] + _v[i]; }
GLfloat operator()(size_t i, size_t j) const { return _u(i,j) + _v(i,j); }
size_t size() const { return _v.size(); }
size_t width() const { return _v.width(); }
size_t height() const { return _v.height(); }
};

How can i make a construtor to check types. I need 3 constructors.

  1. Both are MatExpression
  2. 1st is MatExpression, 2nd is Scalar
  3. 1st is scalar and 2nd is MatExpression

I understand that i need to use type_traits, but i dont understand how to use them.

Marko Taht
  • 1,448
  • 1
  • 20
  • 40
  • As shown, you probably need 3 classes/specializations, as `scalar.size()` doesn't make sense. – Jarod42 May 22 '20 at 14:11
  • Well if i can make 3 constructors. then for constryctors with scalar i can remove the size assertion. And the size assertion actually needs to be improved. In java i could do (E1 implements MatExpression) whats the equivalent in C++? – Marko Taht May 22 '20 at 14:24
  • I meant `return _v.size()` would be wrong when `_v` is Scalar. – Jarod42 May 22 '20 at 15:17
  • Yes i got it. I meant. If i created 2 more constructors without the .size() requirements. Is there a way for me to force it to choos the correct one? Like if E1 or E2 is scalar? – Marko Taht May 22 '20 at 15:32

0 Answers0