D and C++ currently interoperability status

1.2k Views Asked by At

I'd like to learn D, but I haven't understood well an important thing. How's the current interoperability status between C++ and D?

I'm sure it's impossible to link to C++ compiled binaries, as it doesn't even work with C++ compilers. But what if I have the source code of a C++ library and some D source code. Would it be possible to make them speak? (hopefully in an efficient way).

How about the different C++ versions (C++98, 11, 14, 17 and future versions)?

2

There are 2 best solutions below

1
On BEST ANSWER

D interoperability with C++ has been improved considerably in the last few years. The "Interfacing to C++" section of the "D Specification" is a good start if you want to learn more.You may also want to look at the "magical" dpp project - https://code.dlang.org/packages/dpp .

I do not understand your question about linking... The linker is the same no matter whether you use Assembly, C, C++, D, or any combination of them... You will be able to link C++ libraries, but you may not be able to use all what is inside (depends on what is in the library). I've seen D apps linked against Boost libraries for an example.

1
On

I'm part of the compiler team and have been working on the C++ interop this summer, to interact with a piece of C++ code written in modern C++14.

C++ is very well supported by D, in fact it probably has one of the best support for it out there, for a language of this size. You can, for example, throw any exception derived from std::exception in C++ and catch it on the D side. You can write a class in D and use it from D (virtual method or not), or the other way around. You can subclass a D class from C++, and you can subclass a C++ class from D. You can call instantiated templated functions!

Now, there are a few catches:

  • If you are doing something not very idiomatic in C++, e.g. throwing int, that's going to be a problem.
  • The C++ code should not break D's type system. E.g. you cannot represent a char* const* (pointer to constant pointer to mutable char) because D's const is transitive, so it has to be const char* const*.
  • The cross-version support is still not optimal. There is currently a mix of C++98 and C++11, and no way to control what to use. It does not matter in 95% of the cases, since D interfaces at the binary level, and is only concerned about calling convention and symbol mangling.

As for platforms, Windows is very well supported (we have some game devs using it). POSIX support for calling templated functions is more recent, but works for me.

That's it for the language support. The tools around it (e.g. what Dejan mentioned) are still being built, and there's a big interest from the industry in having them, as D is regarded as much more pleasant to use, and it can offer an easy transition path for C++ developers. In fact, all 3 D compilers (DMD, GDC, LDC) use a common frontend written in D, and exposed to C++ their backend (DMC, GCC, LLVM, respectively).

TL;DR:

  • If you want to call C++ and can spare the time to write some bindings in D for your classes / functions, yes.
  • If you cannot write the bindings, stay tuned.
  • If you want to expose D code to C++, a resounding yes!