C++ to Zig?

Zig can compile to C via -ofmt=c and vice versa with translate-c. I wonder if Zig could compile to C++ and/or C++ compile to Zig. C++ classes could be implemented as a zig struct for example.

Hi, here’re my two cents:

  • Being able to compile C++ to Zig would basically require implementing C++ in Zig, which I doubt anyone would want to take on.

  • Since C is (mostly) a subset of C++, compiling Zig to C++ is possible with -ofmt=c.

1 Like

You could compile C++ to WASM via zig c++, then translate the WASM to C via wasm2c (wabt/wasm2c/README.md at main · WebAssembly/wabt · GitHub), then translate the C output to Zig via zig translate-c.

But even if that works the resulting Zig (and C) code will have absolutely nothing in common with the original C++ code, and it will rewrite all memory accesses to bounds-checked array accesses, so apart from being an interesting experiment it’s probably not very useful :wink:

5 Likes