D - functions, delegates and templates
- Carlos Santander B. (33/33) Mar 05 2003 I have never used delegates in my life. Now I'm trying to start with D. ...
- Mike Wynn (39/72) Mar 05 2003 a delegate is an object and the method to call (unlike a C++ pointer to
I have never used delegates in my life. Now I'm trying to start with D. I
think this should be legal:
template gen (T) {
T max (T a,T b) {
return (a>b?a:b);
}
bit comp(T a,T b,T delegate (T,T) f) { /*************/
return a==f(a,b);
}
}
instance gen(int) intGen;
instance gen(real) realGen;
instance gen(char[]) strGen;
void main() {
int a=45,b=32;
real c=-12.3,d=2.4;
char[] e='hola',f='chao';
intGen.comp(a,b,intGen.max);
realGen.comp(c,d,realGen.max);
strGen.comp(e,f,strGen.max);
}
If I change delegate for function in the comp function, it doesn't work
either. Both ways, I get non-matching arguments. Is the code correct or am I
missing something?
The workaround that I've tried was to declare as a function, and also
declare 3 function pointers in main pointing to each max function, but I
think there must be another way to do it.
-------------------------
Carlos Santander
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.459 / Virus Database: 258 - Release Date: 2003-02-25
Mar 05 2003
a delegate is an object and the method to call (unlike a C++ pointer to
member func)
try this
template gen (T) {
T max (T a,T b) {
return (a>b?a:b);
}
class Maxer {
T getmax( T a, T b ) { return max( a,b ); }
}
alias T (*compfunc)( T, T );
bit comp(T a,T b,compfunc f) { /*************/
return a==f(a,b);
}
alias T delegate( T, T ) compdel;
bit comp(T a,T b, compdel f) { /*************/
return a==f(a,b);
}
}
instance gen(int) intGen;
instance gen(real) realGen;
instance gen(char[]) strGen;
int main( char[][] args ) {
int a=45,b=32;
real c=-12.3,d=2.4;
char[] e='hola',f='chao';
intGen.comp(a,b, &intGen.max);
realGen.comp(c,d, &realGen.max);
strGen.comp(e,f, &strGen.max);
intGen.comp(a,b, &(new intGen.Maxer).getmax );
realGen.comp(c,d,&(new realGen.Maxer).getmax );
strGen.comp(e,f, &(new strGen.Maxer).getmax );
return 0;
}
not unlike C/C++ you NEED the & to get a func ptr, or delegate
intGen.max is int(int,int) not int(*)(int,int)
"Carlos Santander B." <carlos8294 msn.com> wrote in message
news:b45tmb$2c47$1 digitaldaemon.com...
I have never used delegates in my life. Now I'm trying to start with D. I
think this should be legal:
template gen (T) {
T max (T a,T b) {
return (a>b?a:b);
}
bit comp(T a,T b,T delegate (T,T) f) { /*************/
return a==f(a,b);
}
}
instance gen(int) intGen;
instance gen(real) realGen;
instance gen(char[]) strGen;
void main() {
int a=45,b=32;
real c=-12.3,d=2.4;
char[] e='hola',f='chao';
intGen.comp(a,b,intGen.max);
realGen.comp(c,d,realGen.max);
strGen.comp(e,f,strGen.max);
}
If I change delegate for function in the comp function, it doesn't work
either. Both ways, I get non-matching arguments. Is the code correct or am
I
missing something?
The workaround that I've tried was to declare as a function, and also
declare 3 function pointers in main pointing to each max function, but I
think there must be another way to do it.
-------------------------
Carlos Santander
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.459 / Virus Database: 258 - Release Date: 2003-02-25
Mar 05 2003








"Mike Wynn" <mike.wynn l8night.co.uk>