hn.today

Comparing reflection capabilities of C++, Zig and C3

nyr24.github.io10 points5 comments
Screenshot of Comparing reflection capabilities of C++, Zig and C3

This compares how C++, Zig, and C3 support compile-time reflection for tasks like enum-to-string conversion, struct introspection, and attribute-driven validation. C3 is introduced as a C/C++-friendly systems language that marks compile-time code with a $ prefix and provides a macro system and tags/attributes for reflection. Examples show C3 enums carrying readable names and attached data, C3 macros iterating over enum values or struct members, and attrdef tags (e.g., @Range) that can be queried inside macros. C++ examples use upcoming reflection facilities (std::meta-like templates, enumerators_of, annotations_of_with_type) and static_assert-driven validation; Zig relies on enum methods, comptime functions/blocks, inline for loops and type-introspection builtins but lacks attribute-style metadata and macros.

The validation example highlights two C3 patterns: a fully compile-time validator annotated with @const, and a runtime validator that still reads compile-time tags, with $if vs if making the compile-time/runtime boundary explicit. The conclusion notes all three support real compile-time reflection but trade ergonomics for power: C++ is powerful but verbose, Zig is modern but lacks attributes, and C3 offers readable, expressive macros and explicit compile-time syntax. The author considers C3 promising and relatively stable, nearing 1.0, and recommends further exploration.

Read on nyr24.github.io5 comments on Hacker News

Summary generated by AI from the linked article. hn.today is not affiliated with Hacker News or Y Combinator.

More in Programming

The daily digest

Today's best Hacker News stories, summarized and screenshotted, one email a day.