C++/D interface: exceptions

deadalnix via Digitalmars-d digitalmars-d at puremagic.com
Fri Sep 12 16:42:48 PDT 2014


On Friday, 12 September 2014 at 17:57:41 UTC, Marc Schütz wrote:
> How about
>
>     try {
>         my_cpp_func();
>     } catch(CppException!(const(char)*) e) {
>         writeln(e.payload.fromStringz());
>     }
>
> ?
>
> Btw, how does implicit conversion work with `catch` in C++? 
> I.e., if you throw a `char*`, will it be caught when you catch 
> `const char*`? This can not be handled easily with such a 
> template, as we would need to catch both 
> `CppException!(const(char)*)` and `CppException!(char*)`.

This is really WAY harder that it seems.

First, you got to understand C++'s RTTI so you can generate the
correct code to match catch blocks. That mean the D compiler must
be able to generate the RTTI symbols for them to link.

Then you got to implement the runtime part of it, considering
there is absolutely NO standardization at all, it is 100%
dependent on implementations details of the C++ standard lib you
are using. You got to retrieve the exception from an opaque
pointer on a structure that is compiler/stdlib dependent and make
sense of it. Then you got to understand the C++ RTTI information
do the proper matching of C++ exceptions with catch blocks's type.

Finally, you have to interface with the C++ runtime to do the
proper call in order to make the C++ runtime aware of what D's
doing. This part is more standardized, so that should be easier.


More information about the Digitalmars-d mailing list