Accessing C++ code from D

Ben Cooley Ben_member at pathlink.com
Thu May 18 13:04:15 PDT 2006


>IMO, Accessing C++ code from D is not a feature that should be supported by the  compiler.
>There has been some attempts to build C++ wrapper generators for D: swig, a
>macro system and I even made one myself (but the memory management is ugly). We just need  C void * pointers. There are (and will be) brilliant people who
>(will) make great tools for D. The impatients (like me) can try to link with the ones they used in C++. We need time. 

Swig works well for what it does.  Generally however you have to devote quite a
bit of time to a swig wrapper to keep it in sync as well.

>I also tried to make a C++ to D translator by modifying Peri's Hankey d2d
>language machine script. It can handle small sized code and output quite
>syntactically correct D code but the semantic is incorrect, especially for
>templates because of the overwhelming "template <T>" syntax. I've given up for
>the moment. A translator is not enough: a C++ compiler to D could eventually >do it but sounds like nonsense.

I don't think a C++ to D translator is possible or even a good idea.  Cpp simply
would not translate into equivalent D idioms except in trivial instances.  That
being said, Cpp/D interop is a completely different animal.  With interop,
you're not trying to translate one idiom into another, instead you're just
making it easier for each to co-exist, and to be able to easily access the
functionality of one from within the other without rewriting.

Microsoft's Cpp/CLI is an excellent example of how interop can work.  Cpp is not
C#, but it allows you to work with C# objects, members, enums, and GC from
within a traditional Cpp program in a reasonably effective manner, and without
having to rewrite your C# code in Cpp, or vicsa versa.

Cpp interop is not "translation".  It would be limited to being able to access
members of a cpp class, call cpp member functions, use C and Cpp macros, and
inline C or Cpp code.  Parsing of Cpp headers would be handled by the Cpp
compiler's parser using GCC-XML.  And compiling of Cpp interop code for
templates, macros, and other complex Cpp elements would be handled by the Cpp
compiler, and accessed in D through C functions.  

For example, with this system, the issues you had with templates simply don't
arise because templates are not converted into D template code, they are
specialized nto standard Cpp classes and compiled with the Cpp compiler, and
those classes are called from the D/Cpp interop system.

>Being a successor of C++ doesn't means that D should support it. Java does not
>support C++ and this lack has not prevent it to spread.

Yes, and yes.  

Yes it certainly should support interop if there is any real intention of being
a successor to Cpp.  

And Yes, interop issues are part of the reason why the new .NET system is
gaining ground against Java.  Both microsoft C# and Cpp have interop with
eachother.  C# has unsafe code, which is completely compatible with C and Cpp,
and Cpp has complete interop with the .NET world, including C#.

Outside of the D forums in the general programming world these issues are not
really controversial.  They are considered self evident and obvious.  This is
why D is not a real alternative to Cpp or C for most programmers.  The successor
to Cpp will not be D, it will be Cpp 0x.









More information about the Digitalmars-d mailing list