[Issue 20110] Module constructor implicitly converts a delegate pointer into a function pointer

d-bugmail at puremagic.com d-bugmail at puremagic.com
Tue Aug 6 06:57:27 UTC 2019


https://issues.dlang.org/show_bug.cgi?id=20110

Simen Kjaeraas <simen.kjaras at gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |simen.kjaras at gmail.com

--- Comment #2 from Simen Kjaeraas <simen.kjaras at gmail.com> ---
There's a difference between &c.test as you use in the assert, and &C.test,
which is what you use in the static this(). The former requires an instance to
serve as the context for the delegate, while the latter does not.

That said, is this behavior useful? Should typeof(&C.test) really be void
function(int, int)? As demonstrated above, it's just plain wrong. The correct
type for typeof(&C.test) is probably void function(int, int, C), as
demonstrated below:

alias int function(int, int, C) Callback;

void passCallback ( Callback cb ) {
    auto c = new C;
    c.i = 30;
    assert(cb(10, 20, c) == 102030);
}

class C {
    int i;

    int test (int foo, int bar) {
        return foo*10000 + bar*100 + i;
    }
}

unittest {
    passCallback(cast(Callback)&C.test);
}

--


More information about the Digitalmars-d-bugs mailing list