Cannot implicitly convert delegate to function

Bruno Medeiros brunodomedeirosATgmail at SPAM.com
Mon May 1 08:48:00 PDT 2006


Thomas Kuehne wrote:
> -----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
> 
> 

But, isn't it like Don said, that this thoroughly depends on the (so far 
undefined) calling conventions used by the compiler?

-- 
Bruno Medeiros - CS/E student
http://www.prowiki.org/wiki4d/wiki.cgi?BrunoMedeiros#D



More information about the Digitalmars-d-learn mailing list