Live data from Hacker News

Cost of enum-to-string: C++26 reflection vs. the old ways

vittorioromeo.com

11–20 of 189 posts

Re: Cost of enum-to-string: C++26 reflection vs. the old ways

#11

No doubt reflection has been built with other use cases in mind, but it sure would have been nice just to have std::to_string(enum)

C++ conference speakers (including keynotes) are now begging everyone to stop using enum to string in their example. While they are a simple and easy to understand example, reflection is for much more interesting problems. I can't think of any other example that I would type into a comment box or put on a slide.

Re: Cost of enum-to-string: C++26 reflection vs. the old ways

#14
post #11

No doubt reflection has been built with other use cases in mind, but it sure would have been nice just to have std::to_string(enum)

C++ conference speakers (including keynotes) are now begging everyone to stop using enum to string in their example. While they are a simple and easy to understand example, reflection is for much more interesting problems. I can't think of any other example that I would type into a comment box or put on a slide.

Anybody the derive traits rust has are a good demo.

Re: Cost of enum-to-string: C++26 reflection vs. the old ways

#15
post #4

I've been wondering about debug-ability of code using reflection. X-Macros are quite annoying to step through in most debuggers, though possible. While the code in the first example is evaluated fully at compile-time, how would you approach debugging it?

I mean it's still C++ that's compiled and executed, surely the compiler would be able to provide a way to hook into that?

Re: Cost of enum-to-string: C++26 reflection vs. the old ways

#17

I can't imagine myself using reflection much, but maybe it will eliminate a lot of feature proposals bogging down the committee and they can focus on harder problems. It would be cool if the stated goal of C++29 was compile times.

I'd argue reflection is very much a feature for libraries. You wouldn't use it directly, but your JSON / YAML serialize is then built on top of it. So are your bindings for scripting engines like Lua.

Re: Cost of enum-to-string: C++26 reflection vs. the old ways

#18
"Enum to string"

We've come full circle huh?

Why do you need this, logging? In that case I would rather reflect the logging statement to pribt any variable name, or hell, just write out the string.

If saving for db, maybe store as string, there's more incentive for an enum in the db, if that's a string you might as well. At any rate it doesn't seem a great idea to depend on a variable name, imagine changing a variable name and stuff breaks.

Re: Cost of enum-to-string: C++26 reflection vs. the old ways

#19
I think the conclusion section should indicate that they are based entirely on GCC 16's behavior and current implementation. We should avoid generalizing one compiler's behavior and performance. Curious how this same test would behave once clang ships C++26 reflection.

Re: Cost of enum-to-string: C++26 reflection vs. the old ways

#20
post #19

I think the conclusion section should indicate that they are based entirely on GCC 16's behavior and current implementation. We should avoid generalizing one compiler's behavior and performance. Curious how this same test would behave once clang ships C++26 reflection.

I was thinking the same thing. Modules are still not widely used, it is a reasonable guess that there are a lot of optimization opportunities left.
Post reply on HN