[Issue 12773] New: Compiler implicitly converts delegate into function when taking an address of a method

via Digitalmars-d-bugs digitalmars-d-bugs at puremagic.com
Tue May 20 02:27:52 PDT 2014


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

          Issue ID: 12773
           Summary: Compiler implicitly converts delegate into function
                    when taking an address of a method
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: critical
          Priority: P1
         Component: DMD
          Assignee: nobody at puremagic.com
          Reporter: andrej.mitrovich at gmail.com

This is extremely nasty. Take a look:

-----
alias Func = void function();

class C
{
    static void foo() { }
    void bar() { }
}

void main()
{
    {
        // Func func = C.foo;  // disallowed, since this is a function call
    }

    {
        Func func = &C.foo;  // ok, the proper syntax usage.
        func();  // ok
    }

    {
        Func func = &C.bar;  // oops, we forgot to mark 'bar' as static!!
        func();  // access violation (requires 'this')
    }
}
-----

The current semantics force us to be extremely careful when using function
pointers. We should ban the implicit conversion of &foo to a function pointer
if this is really a non-static class method. People can use .funcPtr or some
other equivalent instead.

--


More information about the Digitalmars-d-bugs mailing list