[Issue 3720] Taking address of member functions possible without an instance

d-bugmail at puremagic.com d-bugmail at puremagic.com
Tue Dec 5 00:55:35 UTC 2017


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

--- Comment #11 from Mike Franklin <slavo5150 at yahoo.com> ---
A little more information about this issue and the test case:

struct S {
    int a;
    void fun() 
    {
        this.a = 1;
    }
}

void main() 
{
    auto fp = &S.fun;
    fp();
}

There may be 2 schools of thought on how this should behave:
(1) `auto fp = &S.fun;` is trying to get the address of a non-static function
through a type.  Instead, it can be argued that the programmer should have
written `S s; auto fp = &s.fun;` Notice the lower-case `s`.
(2)  `&S.fun` should return a `void function(S* s)` instead of a `void
function()`

If (1) were implemented the compiler would emit an error at `auto fp = &S.fun;`
because it is trying get the address of a non-static `fun` through the type
`S`.

If (2) were implemented the compiler would emit an error at `fp();` because a
context pointer was not supplied as an argument.  In other words it should be
`fp(&s)`.

--


More information about the Digitalmars-d-bugs mailing list