Pointer to C++ member possible?

Bill Baxter dnewsgroup at billbaxter.com
Sun Mar 9 13:38:07 PDT 2008


Jarrett Billingsley wrote:
> "Raynor" <memphis007fr at yahoo.fr> wrote in message 
> news:fr1ach$1qmd$1 at digitalmars.com...
>> I want to use the QT4 library for the GUI and D the D for the core.
>> I think its more simple to make the controllers in C++.
>> I manage to call D methods from C++ and vice versa with C++ 
>> interoperability feature from D 2.0.
>> But i need a signal system so the core can emit signals to the 
>> controllers.
>> Everything is objet so i need pointer to C++ member for the signals.
>>
>> Is there any way to do that?
> 
> There is no standardized ABI for C++ pointers-to-members, making it terribly 
> difficult to interface with them.  You basically have to find out how the 
> particular C++ compiler you're using implements pointers-to-members and 
> implement it, non-portably, using D. 

There's no standardized ABI for name mangling either, but calling a C++ 
mangled name is what D's extern(C++) lets you do.  From what I 
understand it just picks a particular ABI, e.g. DMD uses DMC's ABI. 
If/when GDC supports it, it will use the g++ ABI.

So no standard ABI is not really a reason for not supporting it. 
Handling the non-standard ABI is supposed to be the job of extern(C++).

But seems like lack of a similar concept in D is a problem.  The topic 
of C++-like pointer-to-member has come up before.  I think there is some 
sort of hack to do a D version of it by setting the delegate's .ptr 
property?  Casting to void* and setting it seems to work:

class Foo
{
     private int value;
     this(int v) { value = v; }
     int member() { return value; }
}
void main()
{
     auto x = new Foo(1);
     auto y = new Foo(2);

     auto dg = &x.member;
     writefln("DG -> ", dg());
     dg.ptr = cast(void*)y;
     writefln("DG -> ", dg());
}

But you may be on thin ice with that, since as far as I can see this is 
all the spec has to say about the .ptr on delegates:
   "The .ptr property of a delegate will return the frame pointer value 
as a void*."

So doesn't say anything about /setting/ it.

--bb


More information about the Digitalmars-d-learn mailing list