A question about C++ interop
evilrat
evilrat666 at gmail.com
Sun Mar 29 01:50:24 UTC 2020
On Saturday, 28 March 2020 at 19:14:38 UTC, YD wrote:
>
> Hi, now I have a further question: when the C++ class A
> actually has a method that looks like
>
> virtual void get_info(std::string &s) const = 0;
>
> in order to preserve the virtual function table layout (I found
> that if I omit this function completely in the D declaration,
> and try to use a virtual member function originally defined in
> C++ after this function, the result is core dump), even if I
> don't use this function, in the D file I have to put in line
> like this
>
> abstract void get_info(basic_string!(char) s) const;
Yes, ABI implies that. If you don't use it at all you can just
put a dummy entry like in the following code, otherwise it tries
to call wrong method.
In many cases it will just crash, but this also could introduce
very hard to find bugs when vtable is changed due to class
changes, and the method that you were trying to call landed in
vtable on another method with same signature - Imagine having API
like Database class and instead of dump database it will drop all
tables... phew.
...
// other methods
void vtable_dummy01() {} // or abstract if it's abstract
// other methods
...
> When I try this on Linux (Ubuntu 18.04), the compiler (both dmd
> and ldc2) will complain about "std::__cxx11::basic_string is
> not yet supported", but otherwise the code compiles and links
> correctly, and can run without problem.
STL bindings is unfinished, and don't expect it to be done any
time soon. Tiny fractions of it might be present in Phobos (D
standard library), but this work was done by few people who
really needed that feature and it seems they are now too busy to
continue that work.
>
> So does this mean that there is no way I can interface to this
> C++ API in Windows? Thanks.
Same here, STL bindings is not yet finished. If you don't need
that method specifically, just replace it with a dummy. Or make
your own bindings.
More information about the Digitalmars-d-learn
mailing list