On Fri, 18 May 2012 14:30:46 -0400, Andrei Alexandrescu <seewebsiteforem...@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.

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

Possible (obscure) usage:

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

void x(Object context, void function() f1, void function() f2)
{
   void delegate() dg;
   dg.ptr = cast(void *)context;
   if(uniform(0, 2))
      dg.funcptr = f1;
   else
      dg.funcptr = f2;
   dg();
}

void main()
{
  auto t = new Test;
  x(t, &Test.foo, &Test.bar);
}

Another interesting usage is to test if a function has been overridden:

if((&t.foo).funcptr == &Test.foo)
   writeln("not overridden!");

I personally think the "feature" is too awkward for any real usage. Someone once suggested funcptr and &Test.foo should return void *, so the addresses could be compared, but not used (it's too easy to call this function).

In any case, not a bug.

-Steve

Reply via email to