April 28, 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:


Great example, Thomas. It works, even if I add some parameters to the functions, or when I put the static function outside the class.

It seems trivial to let the compiler allow the implicit conversion.

Thanks,

L.
April 28, 2006
Lionello Lunesu wrote:
> BCS wrote:
>> In article <e2qlrk$1uh5$1@digitaldaemon.com>, Jarrett Billingsley says...
>>> "Lionello Lunesu" <lio@lunesu.remove.com> wrote in message news:e2qig4$1ovk$1@digitaldaemon.com...
>>>> Hi,
>>>>
>>>> Shouldn't a (pointer to a) function be convertible to a delegate? Seems to me it's like casting an int to a long; the latter has "more info" and can be converted back into the former without the loss of data. The same holds for function/delegate: a function could be converted to a delegate for "null"; by forgetting the "this" (null), we get the function pointer back.
>>> Without the context pointer, a delegate cannot function properly.  So casting from delegate to function isn't really possible.
>>>
>>> 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.
>>>
>>
>> It would take some ASM hacking but It might be possible to place the fn ptr in
>> the context pointer and have the delegate's function rearrange the stack and
>> then call (with tail recursion) the function.
> 
> Isn't "this" basically a hidden parameter? A delegate would be a "this" together with a function pointer. Upon calling it would first load/push the hidden parameter before the call to the function.
> 
> The question then is: does "this" interfere with a function's parameter?
> 
> If, like in the MSVC case, "this" is always loaded into register ecx, it would just work: ecx would get "null" and the function gets called with the usual parameters. Since the function does not access [ecx] (it's a function, not a member of any class) the null would not interfere.

This is exactly right. Whether or not a function can be trivially converted to a delegate depends on the calling conventions used by the compiler.

For the gory details, see my article
http://www.codeproject.com/cpp/FastDelegate.asp
which covers C++ member functions and delegates in more detail than anything else on the planet, and shows how ugly things become when a language doesn't define its ABI properly.

-Don.
April 28, 2006
Don Clugston wrote:
> For the gory details, see my article
> http://www.codeproject.com/cpp/FastDelegate.asp
> which covers C++ member functions and delegates in more detail than anything else on the planet, and shows how ugly things become when a language doesn't define its ABI properly.
> 
> -Don.

Interesting article! Thanks for the link ;)

L.
April 28, 2006
I just noticed I got the subject the wrong way around :S
April 30, 2006
pragma wrote:
> In article <e2qv42$2gnt$1@digitaldaemon.com>, Stewart Gordon says...
>> 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.
>>
> 
> Well, there's always the trival solution of declaring all of your "functions" as
> static members of a class - but that's not always possible. ;)

How would that be a solution? Static member functions of a class are free-functions.

-- 
Bruno Medeiros - CS/E student
http://www.prowiki.org/wiki4d/wiki.cgi?BrunoMedeiros#D
May 01, 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
May 01, 2006
In article <e35aik$2pd8$1@digitaldaemon.com>, Bruno Medeiros says...
>
>Thomas Kuehne wrote:
>> 
[...]
>> 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?
>

OTOH the compiler could do this just fine. It HAS to know the calling conventions.


1 2
Next ›   Last »