← Back to context

Comment by pyrolistical

3 months ago

It’s just both using c abi right?

Yeah, this isn't quite C++ interop on its own. It's C++ interop via C, which is an incredibly pertinent qualifier. Since we go through C, opaque pointers are needed for everything, we can't stack allocate C++ values, we need to write extern C wrappers for everything we want to do (like calling member fns), and we don't get any compile-time type/safety checking, due to the opaque pointers.

Direct C++ interop is doable, by embedding Clang into Zig and using its AST, but this is significantly more work and it needs to be done in the Zig compiler. As a Zig user, going through C is about as good as you can do, probably.

  • It's a bit more than your typical "interop via C". With a "sized opaque" type you actually can stack allocate C++ values in Zig (and vice versa stack allocate Zig values in C++), i.e.

    fn stackExample() void {

        var some_cpp_type: c.SomeCppType = undefined;
        c.some_cpp_type_ctor(&some_cpp_type);
        defer c.some_cpp_type_dtor(&some_cpp_type);
    
        // ...
    
    }

Seems like it. And the sizes are all hard-coded, which means you are probably wedded very tightly to a particular C++ compiler.