5 ms·
This seems useful for code generation. Cannot relate this to any previous problems I encountered using c++ Any other interesting use of this?
by justicezyx 5y ago
This seems useful for code generation. Cannot relate this to any previous problems I encountered using c++
Any other interesting use of this?
- bialpio 5y agoSeems that it could be used to solve an old-time classic: generate a mapping from an enum value to its string representation. Another use case that comes to my mind is maybe automating test case generation. Occasionally I was running into situations where I thought code generation could be useful, but not sure what could be a "killer app" equivalent for this feature. Related topic: standardizing reflection in c++. https://youtu.be/ARxj3dfF_h0 https://youtu.be/ARxj3dfF_h0 https://en.cppreference.com/w/cpp/keyword/reflexpr https://en.cppreference.com/w/cpp/keyword/reflexpr
- ggrrhh_ta 5y agothat would be great :-)
- seanbax 5y agoEnum to string is a one-line expression in Circle: https://godbolt.org/z/93f5o77zv https://godbolt.org/z/93f5o77zv Circle has dozens of special traits for accessing useful stuff about types, packs, etc. Don't need to overengineer such a simple thing.
- malkia 5y agoThere is also this approach - https://github.com/Neargye/magic_enum https://github.com/Neargye/magic_enum the trick (relyiong __FUNCTION__ like macros/(funcs?)) is here - https://github.com/Neargye/magic_enum/blob/master/include/magic_enum.hpp#L327 https://github.com/Neargye/magic_enum/blob/master/include/ma... and https://github.com/Neargye/magic_enum/blob/master/include/magic_enum.hpp#L351 https://github.com/Neargye/magic_enum/blob/master/include/ma...
- pisssip 5y agoWhat’s wrong with an array
- rahimiali 5y agoImagine using this to build a balzingly fast serialization library that you’d use like protobufs. Or to explore lots of configurations of your code to search for the variant that runs fastest. Or to write a linear algebra library that doesn’t generate temporary values that are dynamically allocated.
- seanbax 5y agoMy favorite use is putting user-defined attributes on data members, and using reflection to generate a UI to manipulate those values. I do it with these shadertoys: https://github.com/seanbaxter/shaders#reflection-and-attributes-for-shadertoy-development https://github.com/seanbaxter/shaders#reflection-and-attribu... Just mark your declarations up with custom attributes: [[.imgui::range_float { .1, 5 }]] float Zoom = 1.5; [[.imgui::range_float { 0, 1 }]] float Speed = .15; [[.imgui::range_float { .1, 1 }]] float XScale = .3; [[.imgui::range_float { 0, .5 }]] float YScale = .2; Then loop over the members with a meta for, and emit widget code that's guided by the attribute kind and data. These attributes each define a scrollbar. The kind of data you reflect over will likely come from within the program.
- nikki93 5y agoNice! This is definitely my main usecase for reflecty things lately, along with serialization for the data (game engine scenarios). I've been doing static reflection + attributes through this: https://godbolt.org/z/enh8za4ja https://godbolt.org/z/enh8za4ja Which actually comes out to a not-so-bad syntax and the attributes can be read constexpr (some limitations though: simple aggregate-y structs, and all reflected fieds must appear before non-reflected ones). Lately I've been working on a Go (pretty small subset -- no GC or concurrency) to C++ compiler and generating meta stuff as part of it. Mostly because I like how focused Go's syntax is and also it having an official parser and type checker in the standard library makes things pretty quick to get started. I should play with Circle some time! It's definitely touching on a lot of the desired things from a metaprogramming layer.
- jcelerier 5y agoIn c++ there are some hacks that can start to help getting someswhere close (https://ossia.io/posts/reflection/ https://ossia.io/posts/reflection/) but this solution with attributes is what everyone dreams of
- vlovich123 5y agoImagine you want to intern your printf strings somewhere so that your logs look like logit(“some message providing more details. %s, %d”, someStringArg, someIntArg). Typically you have to serialize the whole thing and store it. However, if you save the compile string into a separate database at compile time and the call site transforms to logit(msgId, someStringArg, someIntArg), then you’ve reduced your logging cost to the cost of serializing the log message which is drastically cheaper, faster and takes less space in whatever log storage system you use. You only pay the formatting cost when you go to display the logs to a person.