Re:Zig / C++ Interop
by pyrolistical on Tuesday November 11, 2025 @10:30AM
(#45888404)
It’s just both using c abi right?
[ Parent
| on HN ]
Re:Zig / C++ Interop
by swiftcoder on Tuesday November 11, 2025 @01:09PM
(#45890757)
Seems like it. And the sizes are all hard-coded, which means you are probably wedded very tightly to a particular C++ compiler.
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.
Re:Zig / C++ Interop
by marler8997 on Tuesday November 11, 2025 @04:59PM
(#45893465)
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);
// ...
}