Casting delegates to void*

David Nadlinger see at klickverbot.at
Wed Mar 13 12:18:50 PDT 2013


Hi all,

A quick quiz: Does the following function compile, and if yes,
what will it return?

---
void* delegateToPtr(void delegate() dg) {
     return cast(void*)dg;
}
---

I am working on weeding out the last test suite failures in the
2.062 branch of LDC right now (release to be expected soon(tm)),
and was surprised to find that current DMD indeed accepts the
snippet. The result of the cast is the context pointer of the
delegate.

LDC, on the other hand, refuses to compile the cast. It would be
trivial to change its behavior to match DMD, but I am not sure
why that behavior would be desirable in the first place. It seems
rather like an accepts-invalid bug to me.

An instance of such a cast has been added to the test suite
while fixing issue 7159: 
https://github.com/D-Programming-Language/dmd/blob/18da33b4d303f1dd020576a6a671f91fd6b06c10/test/runnable/xtest46.d#L5011

David


More information about the Digitalmars-d mailing list