Fun with extern(C++)

Manu via Digitalmars-d digitalmars-d at puremagic.com
Tue Jan 26 07:49:24 PST 2016


On 26 January 2016 at 23:58, Jacob Carlborg via Digitalmars-d
<digitalmars-d at puremagic.com> wrote:
> On 2016-01-26 13:59, Manu via Digitalmars-d wrote:
>
>> Hmmm. I wonder if this will cause problems...
>> Is it impossible to support a C++ class implementing a D interface?
>
>
> No that I know how the C++ compatibility works but I would guess the
> compiler needs to know at compile time how to call a method. If it would be
> possible to implement a D interface as either a D class or C++ class. The
> compiler cannot know which runtime type a variable which is declared as an
> interface can hold.
>
> interface A { void foo(); }
> class B : A { void foo(); }
> extern(C++) class C : A { void foo(); } // assuming this works
>
> A a = new B;
> A b = new C; // assuming this works
>
> a.foo();
> b.foo();
>
> When the compiler sees "a" or "b", how should it know it should call "foo"
> as a D method or C++ method? It only knows about the static type which is A,
> a D interface.

I'm not sure there's a difference in the calling convention... the
function being called may perform adjustment of the 'this' pointer
such that it's correct relative to the class that implemented the
function, but that's internal to the function being called.


More information about the Digitalmars-d mailing list