View mode: basic / threaded / horizontal-split · Log in · Help
May 18, 2012
Method pointers are *function* pointers?? Or delegates??
My brain just exploded.
Can someone explain what's going on?

class Test
{
	public void foo() { }
}

static assert(is(typeof(&Test.foo) == void function()));
May 18, 2012
Re: Method pointers are *function* pointers?? Or delegates??
On 18-05-2012 20:22, 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()));

Delegates. Pointer to member function + class instance.

-- 
Alex Rønne Petersen
alex@lycus.org
http://lycus.org
May 18, 2012
Re: Method pointers are *function* pointers?? Or delegates??
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.

Andrei
May 18, 2012
Re: Method pointers are *function* pointers?? Or delegates??
On Friday, 18 May 2012 at 18:30:48 UTC, Andrei Alexandrescu 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.
>
> Andrei

Okay I'll report it... hopefully it isn't just with my version 
though (a little different from official version).
May 18, 2012
Re: Method pointers are *function* pointers?? Or delegates??
On Friday, 18 May 2012 at 18:32:00 UTC, Mehrdad wrote:
> Okay I'll report it... hopefully it isn't just with my version 
> though (a little different from official version).

http://d.puremagic.com/issues/show_bug.cgi?id=8114
May 18, 2012
Re: Method pointers are *function* pointers?? Or delegates??
On Fri, 18 May 2012 14:30:46 -0400, Andrei Alexandrescu  
<SeeWebsiteForEmail@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
May 18, 2012
Re: Method pointers are *function* pointers?? Or delegates??
On Friday, 18 May 2012 at 18:59:23 UTC, Steven Schveighoffer 
wrote:
> On Fri, 18 May 2012 14:30:46 -0400, Andrei Alexandrescu 
> <SeeWebsiteForEmail@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.

I actually realized that might be the reason before I reported 
this, but then I thought:

In that case, shouldn't the 'this' parameter be explicitly part 
of the function (at the end of the parameter list)?
May 18, 2012
Re: Method pointers are *function* pointers?? Or delegates??
On Fri, 18 May 2012 15:17:28 -0400, Mehrdad <wfunction@hotmail.com> wrote:

> On Friday, 18 May 2012 at 18:59:23 UTC, Steven Schveighoffer wrote:
>> On Fri, 18 May 2012 14:30:46 -0400, Andrei Alexandrescu  
>> <SeeWebsiteForEmail@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.
>
> I actually realized that might be the reason before I reported this, but  
> then I thought:
>
> In that case, shouldn't the 'this' parameter be explicitly part of the  
> function (at the end of the parameter list)?

That would be nice, wouldn't it? :)

-Steve
May 18, 2012
Re: Method pointers are *function* pointers?? Or delegates??
On 18-05-2012 21:34, Steven Schveighoffer wrote:
> On Fri, 18 May 2012 15:17:28 -0400, Mehrdad <wfunction@hotmail.com> wrote:
>
>> On Friday, 18 May 2012 at 18:59:23 UTC, Steven Schveighoffer wrote:
>>> On Fri, 18 May 2012 14:30:46 -0400, Andrei Alexandrescu
>>> <SeeWebsiteForEmail@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.
>>
>> I actually realized that might be the reason before I reported this,
>> but then I thought:
>>
>> In that case, shouldn't the 'this' parameter be explicitly part of the
>> function (at the end of the parameter list)?
>
> That would be nice, wouldn't it? :)
>
> -Steve

At least it would prevent writing platform/compiler-specific code...

-- 
Alex Rønne Petersen
alex@lycus.org
http://lycus.org
May 18, 2012
Re: Method pointers are *function* pointers?? Or delegates??
On 5/18/12 1:59 PM, Steven Schveighoffer wrote:
> On Fri, 18 May 2012 14:30:46 -0400, Andrei Alexandrescu
> <SeeWebsiteForEmail@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
« First   ‹ Prev
1 2 3
Top | Discussion index | About this forum | D home