On 2015-01-23 19:43, Andrei Alexandrescu wrote:

On that front, a coworker gave me a simple idea for integration.

1. All C++ non-polymorphic types thrown cannot be caught in D

2. We (or Calypso) define D class counterparts for polymorphic types
thrown from C++, notably std::exception and subtypes. N.B. those are D
classes, not D structs

3. D code can catch std::exception. The only caveat is that the caught
exception cannot survive the catch statement. Given that calling into
C++ is not the paramount of safety in the first place, I'd say that's a
reasonable compromise.

Example:

----
import core.stdcpp.vector;
import core.stdcpp.exception;

core.stdcpp.exception g;

void fun(core.stdcpp.vector!int v)
{
     try
     {
         v.push_back(42);
     }
     catch (core.stdcpp.exception e) // fine, can catch
     {
         g = e; // ouch, will dangle
     }
}
----

Thoughts?

Even with these restrictions C++ exceptions are quite complicated. I've probably said this before but in 64bit Objective-C (which uses the same exception model as C++) you can only catch C++ exceptions with the catch-all block which won't let you access the actual exception:

@try {
    v.push_back(42);
}
@catch (...) { }

Not sure if that makes it particular easier to implement.

--
/Jacob Carlborg

Reply via email to