Different types with the same template argument?

struct Matrix(T, size_t row, size_t col){ alias row Row; alias col Col; auto opBinary(string op, M)(in M m) const if(op == "*"){ static assert(Col == M.Row, "Cannot Mix Matrices Of Different Sizes."); // whatever... return Matrix!(T, Row, M.Col)(); } } void main(){ Matrix!(double, 2, 3) m1 = Matrix!(double, 2, 3)(); Matrix!(double, 3, 2) m2 = Matrix!(double, 3, 2)(); Matrix!(double, 2, 2) m3 = m1 * m2; // ERROR // Error: cannot implicitly convert expression (m1.opBinary(m2)) of type Matrix!(double,row,col) to Matrix!(double,2,2) } 

Why is there a mistake and how can I solve this problem?

+4
source share
1 answer

The problem is that templates are currently being created with their argument types, not their parameter types.

If you changed the return statement to:

 return Matrix!(T, cast(int)Row, cast(int)M.Col)(); 

It will be compiled because it was created using int , not size_t (which is uint or ulong).

This is a long-standing mistake, and although he did not like it earlier, Walter recently changed his mind supporting this change in order to use parameter types. Here is a removal request that fixes this problem (it will be in the next version of DMD), linking various related errors.

+5
source

Source: https://habr.com/ru/post/1386037/


All Articles