C++/D interface: exceptions

Marco Leise via Digitalmars-d digitalmars-d at puremagic.com
Fri Sep 12 13:30:17 PDT 2014


Am Fri, 12 Sep 2014 17:57:39 +0000
schrieb "Marc Schütz" <schuetzm at gmx.net>:

> 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*)`.

No, let's just stick to 1) please :p

Not only const(char) vs. char might be an issue because C's
const doesn't match D's const exactly, but also char can be
defined to be literally anything that can hold at least 8 bits.

http://msdn.microsoft.com/en-us/library/0d294k5z.aspx
http://www.cplusplus.com/reference/climits/
http://en.wikipedia.org/wiki/C_data_types

I know this argument could be made about interfacing with C++
in general, but in case of throwing we do not replicate the
structure of the C++ side in D as we would do with
declarations of structs and classes, so the types would be
thrown from C++ and end up "raw" in D.

I think this requires at least a D compiler flag to name the
C++ compiler eco system to emulate: e.g. MSVC++, ICC, DMC, GCC.
With that information you could then
catch(what_the_c_compiler_understands_by_a_const_unsigned_char_ptr)
or
catch(what_the_c_compiler_understands_by_a_const_signed_char_ptr)
NOT possible:
catch(what_the_c_compiler_understands_by_a_const_char_ptr),
because that would have been remapped by the C++ compiler to
either one of the former (I think).
The raw void* exception data for C char* may be presented inside
the catch block as byte*, ubyte*, ushort* or whatever is known
to match the C compiler specified on the command line.
(I hope my assumption about how C++ compilers mangle exception
 types is not too far off.)

-- 
Marco



More information about the Digitalmars-d mailing list