Does D support member function template?

Thomas Kuehne thomas-dloop at kuehne.cn
Sun Apr 16 12:03:34 PDT 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-----



More information about the Digitalmars-d-learn mailing list