digitalmars.D.learn - Does D support member function template?
- Li Jie <cpunion gmail.com> Apr 13 2006
- Thomas Kuehne <thomas-dloop kuehne.cn> Apr 16 2006
- Li Jie <cpunion gmail.com> Apr 16 2006
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
Apr 13 2006
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 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 -----BEGIN PGP SIGNATURE----- iD8DBQFEQqLS3w+/yD4P9tIRAiuRAKDDhz8rQvR9RvNNQ5dr1695S/sxXwCggY2J UZmiwvY/ij2UnJpFXHi2b1E= =Odkq -----END PGP SIGNATURE-----
Apr 16 2006
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..
Apr 16 2006








Li Jie <cpunion gmail.com>