Thread overview
Updating C++ Interop documentation
Jul 03
Mehdi
Jul 03
Sergey
Jul 13
IchorDev
July 03

Hi D community,

I just wanted to say that I'm a huge fan of the D language, and I think you guys are doing awesome work. Recently, I've been exploring interoperability with C++, and it has been a great learning experience. However, I believe the documentation on this page is incomplete and sometimes misrepresents what is possible.

To stay updated on the recent D language status regarding interop, I watched a d conf video on youtube: "https://www.youtube.com/watch?v=mI6-PmZy-u0&t=2279s". I've also been reading the repository that was discussed in the conference: "https://github.com/bosagora/agora".

I've also been studying the Druntime code, particularly the stdcpp library: "https://github.com/dlang/dmd/tree/master/druntime/src/core/stdcpp". There are also useful tests located in the Druntime repository. However, I feel like the documentation for interfacing with C++ code does not mention the capabilities of the core.stdcpp module and lacks examples of how to use it. I understand that writing documentation takes time, and you guys are busy, but if there is a to-do list somewhere, please consider adding "update docs interfacing with C++" to it.

Thank you for your hard work!

July 03

On Monday, 3 July 2023 at 13:14:47 UTC, Mehdi wrote:

>

Hi D community,
ttps://github.com/dlang/dmd/tree/master/druntime/src/core/stdcpp". There are also useful tests located in the Druntime repository. However, I feel like the documentation for interfacing with C++ code does not mention the capabilities of the core.stdcpp module and lacks examples of how to use it. I understand that writing documentation takes time, and you guys are busy, but if there is a to-do list somewhere, please consider adding "update docs interfacing with C++" to it.

Thank you for your hard work!

It seems you already have a good knowledge of what inside this module. Probably you can create a PR with some details. And maybe someone else will join you in documentation update.

July 13

On Monday, 3 July 2023 at 13:41:45 UTC, Sergey wrote:

>

It seems you already have a good knowledge of what inside this module. Probably you can create a PR with some details. And maybe someone else will join you in documentation update.

I should’ve probably chipped in by now. I’ve used extern(C++) a lot and I have a lot of info I could share by now.

Here’s a few notable errors and omissions in the spec and in “Interfacing to C++”:

A: The spec fails to provide examples for the string-namespace style of extern: extern(C++, "one", "two")

B:

>

At present, C++ exceptions cannot be caught in or thrown from D, and D exceptions cannot be caught in or thrown from C++.

This is false. D’s spec also begs to differ: https://dlang.org/spec/statement.html#catching_cpp_class_objects

C: The spec fails to mention the compiler flag for setting which C++ standard to link against.

D:

>

D cannot directly call C++ special member functions, and vice versa. These include constructors, destructors, conversion operators, operator overloading, and allocators.

D can call C++ constructors and destructors. Not sure about the others.

E: Interfacing to C++ says that using "extern(C++) class" for a C++ struct or class is okay. It fails to clarify that C++‘s classes are value types, unlike in D where a class instance is inherently a pointer, and cannot be made into a value. I’m unsure of whether D’s C++ interoperability accounts for this difference, or whether this is an omission.

Additionally, D disallows default constructors, but I think when they’re extern(C++) they should be allowed just for the sake of ease-of-linking. As it is, you can create a constructor with a dummy argument and use pragma(mangle) to manually fix it, but this is pretty confusing to end-users, where Type x = Type() isn’t valid code (yet it compiles), but Type x = Type(0) is.