Cannot implicitly convert delegate to function

Thomas Kuehne thomas-dloop at kuehne.cn
Thu Apr 27 12:01:10 PDT 2006


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Stewart Gordon schrieb am 2006-04-27:
> Jarrett Billingsley wrote:
><snip>
>> The other way around.. I don't think so either.  Something tells me the 
>> mechanism for a delegate call would screw up if the context pointer were 
>> null, or if the function weren't designed to be a delegate. 
>
> But why?  I can't understand why a function that doesn't touch the 
> context pointer at all can't be trivially be converted to a delegate in 
> which the context pointer is null.

Let's try it:

# class C{
#     void foo(){
#         printf("foo\n");
#     }
# 
#     static void bar(){
#         printf("bar\n");
#     }
# }
# 
# int main(){
#     C c = new C();
# 
#     void delegate() d = &c.foo;
#     void* p = cast(void*)&d;
#     int* i = cast(int*) p;
# 
#     printf("delegate:\n");
#     printf("%08X (instance)\n", c);
#     printf("%08X %08X (raw delegate)\n", i[0], i[1]);
#     d();
# 
#     printf("\nfunction:\n");
#     void function() f = &C.bar;
#     p = cast(void*)&f;
#     int* j = cast(int*) p;
#     printf("%08X (function)\n", &C.bar);
#     printf("%08X (raw function)\n", j[0]);
#     f();
# 
#     printf("\nfunction -> instance delegate\n");
#     i[1] = j[0];
#     printf("%08X (instance)\n", c);
#     printf("%08X %08X (raw delegate)\n", i[0], i[1]);
#     d();
# 
#     printf("\nfunction -> null delegate\n");
#     i[0] = 0;
#     printf("%08X %08X (raw delegate)\n", i[0], i[1]);
#     d();
# 
#     return 0;
# }


delegate:
55719FE0 (instance)
55719FE0 08049DF4 (raw delegate)
foo

function:
08049E0C (function)
08049E0C (raw function)
bar

function -> instance delegate
55719FE0 (instance)
55719FE0 08049E0C (raw delegate)
bar

function -> null delegate
00000000 08049E0C (raw delegate)
bar

I don't see any problems - except for missing compiler support.

Thomas


-----BEGIN PGP SIGNATURE-----

iD8DBQFEUSI03w+/yD4P9tIRAqZ+AJ9OsUv0mjgIcQCdLYV8gq9EQTdi/QCgp1TJ
a4BV+qlS5NOAZrbQelm6vas=
=+G5k
-----END PGP SIGNATURE-----



More information about the Digitalmars-d-learn mailing list