[Issue 17080] Can assign member-function-ptr to free-function-ptr

via Digitalmars-d-bugs digitalmars-d-bugs at puremagic.com
Wed Jan 11 10:59:33 PST 2017


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

--- Comment #4 from Sprink <sprink.noreply at gmail.com> ---
(In reply to Marenz from comment #3)
> I have been encouraged by Uplink|DMD to reopen this issue as it differs in a
> few aspects to the one it is marked a duplicate of:
> 
> The issue 3720 should still compile because there isn't anything wrong with
> taking the address of a delegate, even without the instance (it would simply
> have .this (or whatever the name is) set to null.
> 
> My example however shows an implicit conversion from function -> delegate
> and that should in no case compile.

It is the same issue. If issue 3720 is fixed, then this issue wouldn't exist.

    auto fp = &S.fun;
    fp();

This is not valid code and only works because "auto" evaluates to a function.
The use case for taking a pointer of a member function is to use it with
objects, thus a delegate isn't suited here either.


    auto fp = &S.fun; // this line would need to stay the same

Now it's a matter how to call it? C++ uses a different syntax.

    S s;
    (s.*fp)();

In either case it is the exact same problem, that taking the address of a
member function without an instance of that object results in the type being a
regular function pointer.

It's not an implicit conversion from function -> delegate, the compiler
incorrectly defines it as a function.

    writeln(typeof(fp).stringof); // prints void function(), not delegate

A bunch of other issues that illustrate the same thing are also marked
duplicates.
https://issues.dlang.org/show_bug.cgi?id=12154
https://issues.dlang.org/show_bug.cgi?id=12773

--


More information about the Digitalmars-d-bugs mailing list