How to pass a class by (const) reference to C++

Jan Jan at
Wed Dec 15 09:36:54 UTC 2021

On Tuesday, 14 December 2021 at 07:50:48 UTC, evilrat wrote:
> There is some missing features like above tail ref and const, 
> there is minor mangling issues that requires pragma mangle 
> sometimes, and other annoying little details.

As far as I can tell from my limited experience, the way D 
approaches interacting with C++ is sound and extremely useful. 
Link compatibility goes a very long way.

Unfortunately it's the "annoying little details" that I 
immediately bumped into. Things that should be no problem at all 
with just link compatibility, like incorrectly mangled functions 
(forgetting about 'const' in return types on Windows) and this 
very, very annoying issue that I can't pass a class by reference 
but only by pointer.

I can understand that passing a class by value might be out of 
scope (though it would be possible, since it works for structs 
just as well), but with pass by reference it's really just D's 
boneheadedness to try to have its own way.

D is a great language, and I want to use it, but as a stand-alone 
language it just doesn't have the ecosystem that I need 
(gamedev). Using it as an integrated scripting language is for me 
the next best thing to benefit from it in a project that is 
otherwise C++. With link compatibility and auto-generating some 
bindings, I can get there eventually, but it's still a lot of 
work with many manually crafted shims. I am currently willing to 
invest a lot of time to try to solve this, but I won't rewrite a 
huge C++ code base just because D can't pass classes by reference.

Other projects will have even more constraints and even less 
willingness to invest time into such an undertaking and just 
scrap the idea early on.

C++ link compatibility was a great idea, but if D wants to expand 
it's user base further, in my opinion it has to polish the 
interop and be willing to make a few sacrifices on it's end, 
because that can save everyone else hundreds of hours of tedious 
work (and maintenance) and thus be the deciding factor for or 
against using D in a C++ project.

More information about the Digitalmars-d-learn mailing list