Yes, that will work fine as long as the C code never needs to see "inside" the handle_tag
structure, and the appropriate C++ construction/destruction is performed by the C++ code (which I preseume the construct
and destruct
are for).
All that the C code needs is a pointer to some datastructure - it won't know what the contents is, so the content can be anything you like it to to be, including constructor/destructor reliant data.
Edit:
I should point out that this, or methods similar to it (e.g. using a void *
to record the address of an object for the C portion to hold
), is a fairly common way to interface C-code to C++ functionality.
Edit2:
It is critical that the C++ code called doesn't "leak" exceptions into the C code. That is undefined behaviour, and very much liable to cause crashes, or worse, "weird things" that don't crash... So unless the code is guaranteed to not cause exceptions (and for example std::string
is liable to throw bad_alloc
in case of low memory), it is required to use a try/catch
block inside code like construct
anf func
in the C++ side.