Are D classes proper reference types?

Ola Fosheim Grøstad ola.fosheim.grostad at gmail.com
Sun Jun 27 07:54:38 UTC 2021


On Saturday, 26 June 2021 at 20:03:01 UTC, kinke wrote:
> On Saturday, 26 June 2021 at 13:49:25 UTC, Ola Fosheim Grøstad 
> wrote:
>> Is it possible to inherit from a C++ class and get a D 
>> subclass, and is it possible to inherit from a D class and get 
>> a C++ class?
>
> Sure thing, with `extern(C++) class` of course.

That is all good, but it will lead to `extern(C++) class` 
replacing D classes. So why not unify right away? Why wait for 
the inevitable?

> With C++, you can today, an `extern(C++) class C` is equivalent 
> to and mangled as C++ `C*`. You can't pass it directly to some 
> `unique_ptr<C>` or `shared_ptr<T>` of course; an according D 
> wrapper reflecting the C++ implementation (library-dependent) 
> would be needed anyway for correct mangling. It'd be 
> implemented as a templated D struct

Yes, this is all good. But if you unify the layout of C++ and D 
classes and use the same layout as C++ shared_ptr for reference 
counted D classes then you can easily move back and forth between 
the languages. I think the presumption that development only 
happens in D and you only use other people's C++ code is ill 
advised. One may use a framework in C++ that one also extend in 
C++, but maybe want to use another language for the high level 
stuff.




More information about the Digitalmars-d-learn mailing list