What Field Enumeration Costs to Compile in C++26: Native Reflection, Structured-Binding Packs, Reflection Libraries and Hand-Written Serializers on GCC 16
Abstract
C++26 adds standard ways to enumerate a struct's fields, among them static reflection (P2996, with P3394 annotations) and structured-binding packs (P1061), alongside hand-written code, X-macros and libraries (Boost.PFR, reflect-cpp, Glaze) that now ship pre-C++26 and C++26 paths. We time each in a struct serializer with byte-identical output on GCC 16.2.0, at the front end and at -O2, in randomized interleaved rounds with code-level checks of every single-factor contrast. At the front end, inline native enumeration in the form used in several of P2996's own examples, an expansion statement over the std::span of define_static_array, costs about as much per struct of 16 scalar fields as Boost.PFR and 7.96x a P1061 pack, itself about as cheap as hand-written code. The same loop over a constant array costs 0.47x as much: about half of the span form's cost comes with the span. The ratio to P1061 shrinks on a heterogeneous type graph and at -O2. In a whole translation unit at the front end the library outweighs the mechanism: 200 structs take 0.59 s with inline native enumeration, 0.56 s with Boost.PFR and 3.86-4.13 s with the C++26 paths of reflect-cpp and Glaze, whose visitors also compute field names. Per type, each library's C++26 path is cheaper than its pre-C++26 path at 16 fields, but Glaze's gain reverses at 128 fields (pinned release v9.1.0); Boost.PFR's C++17 path grows almost quadratically with the number of types. Hand-written serializers sharing one overloaded name grow superlinearly with the number of types. The -freflection flag alone makes reflection-free code up to 1.264x costlier per type on GCC 16.2.0 and up to 2.578x on 16.1.0, so comparisons between mechanisms depend on the point release. Part of the span form's cost grows with the number of structs, which we trace to GCC registering block-scope statics in quadratic time (reported as GCC PR128928).
- Comment
- 21 pages, 5 figures, 6 tables. Code, raw per-run data and analysis scripts: https://doi.org/10.5281/zenodo.24443567 (GitHub: https://github.com/DenysKachalenko/CPP26FieldEnumerationCompileCostArtifact)