r/cpp • u/SuperV1234 https://romeo.training | C++ Mentoring & Consulting • May 12 '26
cost of enum-to-string: C++26 reflection vs the old ways
https://vittorioromeo.com/index/blog/refl_enum_to_string.html
178
Upvotes
52
u/azswcowboy May 12 '26
Except realistic programs that include many standard library headers anyway won’t notice the meta header addition at all likely. The original thesis only applies if you don’t include vector or string in the first place.
42
u/TopIdler May 12 '26 edited May 12 '26
Does it compile to the same code? e.g. what about the runtime performance of the actual enum to string?
edit: forgot to say; great article! Thank you.
9
u/tisti May 12 '26
The modules comparison is a bit flawed when extrapolation to "real-world projects". I'd expect that replacing all std header includes with a single import in each TU would be a net gain on a heavy-ish project.
Haven't touched modules so far due to tooling not being there yet, but seems we are at the watershed.
5
u/BarryRevzin May 14 '26
Just to make sure I understand — you're posting a benchmark which shows that, for the very first production release of reflection in a compiler, if you look at the worst case timing (including
<meta>in a TU that includes nothing), that including all of reflection is only very marginally slower than including a dedicated enum-to-string library, but incrementally performs better than it? And this is conveyed as a negative? Coupled with a very strange, fear-mongering benchmark showing that a purely serial build of a 500-TU project that does nothing but include<meta>will take a while?Okay.
11
u/daniel_nielsen May 12 '26
Many thanks for this. I made the same discovery this week PCH > Modules. Both faster and easier to use, but I thought I had done something wrong until I read your article.
15
u/friedkeenan May 12 '26
I'd be interested to see this done with GCC 16.2, which still has yet to release. But it has at least one speedup, which solves this bug: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=125179
I had thought there was another bug pertaining to modules and reflection being slow together, but I'm now not able to find it so I might just be completely making that up.
4
u/Tringi github.com/tringi May 12 '26
Finally some good content!
3
u/Nolia_X May 12 '26
Do not use template for for this, use an std::array and index it. You can generate such array at compile time using reflection