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

Jan Jan at Krassnigg.de
Mon Dec 13 15:21:19 UTC 2021


On Monday, 13 December 2021 at 13:02:50 UTC, Ola Fosheim Grøstad 
wrote:
> On Monday, 13 December 2021 at 12:51:17 UTC, evilrat wrote:
>> That example is still looks very conspicuous because it is 
>> very likely does nothing on the caller side in C++ as it 
>> passes a copy.
>
> Yes, I wouldn't want to use it, maybe manual mangling is 
> better, but still painful. ```const A&``` is so common in C++ 
> API's that it really should be supported out-of-the-box.  All 
> it takes is adding a deref-type-constructor to the D language 
> spec, e.g. ```ref const(@deref(A))```

I fully agree. This pattern is so common in C++, that I am 
surprised D doesn't have a way to do this already. The whole idea 
of linking against C++ is to interop easily, with little friction 
and high performance. Needing to build any shims or redesign the 
C++ side is very much contrary to this goal.

Does anyone know whether such issues have been discussed before? 
I can't imagine I'm the first one to run into this.


More information about the Digitalmars-d-learn mailing list