Method pointers are *function* pointers?? Or delegates??

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Fri May 18 13:35:38 PDT 2012


On 5/18/12 1:59 PM, Steven Schveighoffer wrote:
> On Fri, 18 May 2012 14:30:46 -0400, Andrei Alexandrescu
> <SeeWebsiteForEmail at erdani.org> wrote:
>
>> On 5/18/12 1:22 PM, Mehrdad wrote:
>>> My brain just exploded.
>>> Can someone explain what's going on?
>>>
>>> class Test
>>> {
>>> public void foo() { }
>>> }
>>>
>>> static assert(is(typeof(&Test.foo) == void function()));
>>
>> Looks like a bug. The assert should pass only if foo were static.
>
> No, this is not a bug.

It is.

> The purpose is so you can get the function pointer portion of a delegate
> without an instance of the object.

Typing is what it is. The following program is unsound without a cast in 
sight:

class Test
{
     void foo() { writeln("foo"); }
}

static assert(is(typeof(&Test.foo) == void function()));

void fun()
{
     writeln("fun");
}

void main() {
     alias void function() TFun;
     TFun a = &fun;
     a();
     a = &Test.foo;
     a();
}

At best things could be arranged that &Test.foo has type void 
function(Test) or something.


Andrei


More information about the Digitalmars-d mailing list