Deducing types from member function template

Li Jie cpunion at gmail.com
Sat Dec 2 04:52:12 PST 2006


I want to write a Matrix class, a c++ version looks like:

[code]
template <class T, int R, int C>
class Matrix
{
};

template <class T, int R, int C, int C1>
Matrix<T,R,C1> operator* (const Matrix<T,R,C>& lhs, const Matrix<T,C,C1>& rhs)
{
    return Matrix<T,R,C1>();
}

int main()
{
    Matrix<int,3,4> m1;
    Matrix<int,4,3> m2;
    Matrix<int,3,3> m3 = m1 * m2;
    Matrix<int,2,3> m4;
    // m1 * m4;  <== MUST compile fail

    return 0;
}
[/code]

It can be compiled. D version:

[code]
class Matrix(T, int R, int C){
    static assert(R > 0 && C > 0);

    Matrix!(T,R,C1) opMul(int C1)(Matrix!(T,C,C1) rhs){
        return null;
    }
}


int main()
{
    Matrix!(int,3,4) m1;
    Matrix!(int,4,3) m2;
    // auto m3 = m1 * m2;  <== NEED compile success
    Matrix!(int,3,3) m3 = m1.opMul!(3)(m2);
    Matrix!(int,2,3) m4;
    // m1 * m4;  <== MUST compile fail

    return 0;
}
[/code]

"m1 * m2" can't be compiled:

matrix.d(14): template matrix.Matrix!(int,3,4).Matrix.opMul(int C1) does not
match any template declaration
matrix.d(14): template matrix.Matrix!(int,3,4).Matrix.opMul(int C1) cannot
deduce template function from argument types (Matrix)
matrix.d(14): template matrix.Matrix!(int,3,4).Matrix.opMul(int C1) does not
match any template declaration
matrix.d(14): template matrix.Matrix!(int,3,4).Matrix.opMul(int C1) cannot
deduce template function from argument types (Matrix)


I must write "m1.opMul!(3)(m2)". Can you help me?



More information about the Digitalmars-d-learn mailing list