C++ to D

Jeff Jones via Digitalmars-d digitalmars-d at puremagic.com
Thu Apr 2 09:44:27 PDT 2015


On Thursday, 2 April 2015 at 12:15:31 UTC, Daniel Murphy wrote:
> "017hnoor"  wrote in message 
> news:vxieyipmafnyksquehfq at forum.dlang.org...
>
>> I'm mainly talking about a simple
>> code based solution but I'm also curious about how easily this 
>> would be by compiler modification.
>
> It would be easier to comment on if I knew what problem you 
> were trying to solve.  Porting an application?  D plugin for 
> C++ app?  C++ library D app?

NOOOO!!!!

You are completely off your rocker!! I'm not trying to solve any 
problems! I'm simply curious if the above is possible?

We know we can bind D to C but if we could write C++ code that is 
compatible with D at the binary level then maybe it would be 
easier to bind C to D?

IIRC the issue was that C++'s class layout doesn't have a 
metadata pointer and it's vtable is just a list(can't contain 
pointers to other vtables or whatever).



But suppose it was possible to "finagle" C++ so that we had the 
metadata pointer in the right place.


e.g., suppose we could do something like this in C++:


class CWrapper
{
  ....
     void* operator new(std::size_t sz)
     {
       // e.g.,
       int size = sizeof(CWrapper) + 4;
       void* p = malloc(size);
       p[0] = CWrapperMetaDataPtr;
       return p+2*4;
   }
}

So, the idea with the above scratch-code is that we have CWrapper 
laid out like

MetaData ptr
vtable ptr
CWrapper


Which, I think is now similar to D. In theory, if the metadata is 
correct then D should be able to use CWrapper directly as it 
would then look like a D Class?

We could even probably mimic the build in operators in D in C++? 
E.g., opdispatch can be added. Obviously the C++ compiler has no 
idea about that so we would have to call it explicitly and this 
may be bug prone(confusing C++ with D) but we could call it 
explicitly if we need it on the C++ side.


Anyways, Just an idea. I'm curious to how well it well and easy 
it would work. There is no real point at it, no application, etc. 
Just call it a "thought experiment".


More information about the Digitalmars-d mailing list