On 03/20/2017 05:55 PM, StarGrazer wrote:
typeof(&method) fails unless method is static. Says & requires this.

Works for me:

----
class C
{
    void method() {}
    typeof(&method) x;
}
typeof(&C.method) y;
----

Tested with dmd 2.073.2.

Note that the type of x and y is `void function()`, not `void delegate()`. That's quite awful. In my opinion, `&method` shouldn't work like this, but it does.

But for typeof, it shouldn't matter and should pass.

I disagree. `typeof(foo)` should only work when `foo` works. And `&method` shouldn't work when there's no `this`.

So how to get a function pointer to a non static member function(of an
interface)?

I've tried creating the type like

T t;
typeof(&t.method) fptr;

but same issue. It may be because T is an interface, but again, it
shouldn't matter. I just want the function pointer declaration.

Works with an interface, too:

----
interface T { void method(); }

T t;
typeof(&t.method) fptr1;
typeof(&T.method) fptr2;
----

Here, fptr1 has type `void delegate()` which is ok, but fptr2 has type `void function()` which is pretty bad. So, what you tried compiles for me and should work.

To keep it more hygienic, you can put the `T t;` and `typeof(&t.method)` in an immediately called function literal:

----
typeof(() { T t; return &t.method; } ()) fptr3;
----

e.g., `void function();` for `void foo();`

Really should be `void delegate()`. With the `function` type you're losing the `this` pointer.

Reply via email to