Thread overview | |||||
---|---|---|---|---|---|
|
April 14, 2006 Does D support member function template? | ||||
---|---|---|---|---|
| ||||
I write a matrix class: # class Matrix(T, int R, int C) { # //... # } How to write the opMul? It uses like this: # Matrix!(int, 3, 4) m1; # Matrix!(int, 4, 8) m2; # Matrix!(int, 3, 8) m3 = m1 * m2; Thanks, - Li Jie |
April 16, 2006 Re: Does D support member function template? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Li Jie Attachments: | Li Jie schrieb am 2006-04-14:
> I write a matrix class:
>
> # class Matrix(T, int R, int C) {
> # //...
> # }
>
> How to write the opMul? It uses like this:
>
> # Matrix!(int, 3, 4) m1;
> # Matrix!(int, 4, 8) m2;
> # Matrix!(int, 3, 8) m3 = m1 * m2;
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
|
April 17, 2006 Re: Does D support member function template? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Thomas Kuehne | In article <ifkah3-dl7.ln1@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.. |
Copyright © 1999-2021 by the D Language Foundation