Does D support member function template?

Li Jie cpunion at gmail.com
Sun Apr 16 18:16:51 PDT 2006


In article <ifkah3-dl7.ln1 at birke.kuehne.cn>, Thomas Kuehne says...
>I think that isn't (yet) possible.
>
>If there are many different matrix sizes you may try:
>
># class Matrix(T){
>#     this(T[][] data){
>#         // ...
>#     }
># 
>#     typeof(this) opMul(typeof(this) arg){
>#         typeof(this) result;
>#         // ...
>#         return result;
>#     }
># 
>#     Matrix!(T) opDiv(Matrix!(T) arg){
>#         Matrix!(T) result;
>#         // ...
>#         return result;
>#     }
># }
># 
># int main(){
>#     Matrix!(int) a;
>#     Matrix!(int) b;
>#     // ...
>#     auto c = a * b;
>#     Matrix!(int) d = a / b;
># 
>#     return 0;
># }
>
>
>If you only deal with a few matrix sizes you may try:
>
># template opMul(int R2, int C2){
>#     Matrix!(T, R, C2) opMul(Matrix!(T, R2, C2) arg){
>#         Matrix!(T, R, C2) result;
>#         // ...
>#         return result; 
>#     }
># }
># 
># class Matrix(T, int R, int C){
>#     // ...
># 
>#     mixin opMul!(4, 8);
>#     // and other mixins
># }
># 
># int main(){
>#     Matrix!(int, 3, 4) a;
>#     Matrix!(int, 4, 8) b;
>#     // ...
>#     auto c = a * b;
># 
>#     return 0;
># }
>
>Thomas

Thanks Thomas.
Very depressing, Aha.. I think that only this way:

# template dotMul(T, int R, int C, int C1){
#   Matrix!(T,R,C1) dotMul (Matrix!(T,R,C) lhs, Matrix!(T,C,C1) rhs) {
#     // ...
#   }
# }
# 
# auto m1 = Matrix!(int,3,4);
# auto m2 = Matrix!(int,4,8);
# auto m3 = dotMul!(int,3,4,8)(m1,m2); // OK
# auto m4 = dotMul(m1,m2); // Compile error..





More information about the Digitalmars-d-learn mailing list