4 ms·
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)
by HarHarVeryFunny 6mo ago
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)
- bluGill 6mo agoC++ 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.
- surajrmal 6mo agoAnybody the derive traits rust has are a good demo.
- cogman10 6mo agoIt comes up pretty frequently in java. Serialization/Deserialization, adding capabilities based on type, Adding new capabilities to a type, general tuning (for example, adding a timing or logging call onto methods). Almost all the Java web frameworks are giant balls of reflection. Name a function the right way or add the right magic annotation and the framework will autowire it correctly. It's a pretty powerful tool. (IDK if C++'s reflection is as capable, but this is what was enabled by java's reflection).
- david422 6mo ago> Almost all the Java web frameworks are giant balls of reflection. Name a function the right way or add the right magic annotation and the framework will autowire it correctly. I find this to be very powerful, and also very unintuitive/undiscoverable at the same time.
- kuboble 6mo agoReflection is simply a syntax vinegar for duck typing.
- cogman10 6mo agoInitially, but it very quickly becomes discoverable once you are familiar with how things are working. Most frameworks in Java are very similar. The ones that aren't are effectively doing what "expressjs" does in terms of setup, which is still pretty discoverable. Most java frameworks rely on annotations rather than naming schemes which makes everything a lot easier to grok.
- SuperV1234 6mo agoJava reflection is another beast altogether as it is runtime reflection. C++26 reflection is purely compile-time, which not only means it adds zero runtime cost, but also prevents those kind-of-insane use cases you see in Java and C#.
- pjmlp 6mo agoI think C++ devs have to eventually update their knowledge how Java and .NET work when talking about reflection. Yes, originally they only supported runtime reflection. Nowadays they have compile time tooling as well, via plugins, annotation processors, and code generators. Which is exactly how you can have a Spring like frameworks that do all the AOP magic at compile time, for native code with GraalVM or OpenJ9, like Quarkus or Micronaut.
- maccard 6mo agoSerialization is the canonical example. Being able to turn struct MyStruct { int val = 42; string name = "my name"; }; into { "val": 42, // if JSON had integers, and comments of course "name": "my name", } is incredibly powerfuly. If reflection supported attributes (i can't believe it shipped without, honestly), then you could also mark members as [[ignore]] and skip them.
- bluGill 6mo agoIt is powerful, but I'm not sure it is a good idea. Other languages have it, and there is lots of experience in all the ways things go wrong in the real world. I'm inclined to say you should hand write this code because eventually you will discover something weird anyway.
- SuperV1234 6mo agoI think this is a very bad take -- once you write it by hand you have to manually keep it in sync with the actual struct and ensure you made no mistakes. Reflection guarantees 1-1 future-proof mapping with the actual C++ struct, avoids boilerplate, and ensures that the serialization logic is correct.
- bluGill 6mo agoThe protocol is important though, not the internal structure. When you only have exactly one version of a program talking to the same version of itself you don't care. However when you are mixing versions or worse programming language (and thus can't mix structs which are implementation details of your language) the protocol is what matters. That is if you are worried about doing this by hand reflection is not the answer, something like protobuf where your data structures are generated is the answer.
- gpderetta 6mo agoI completely understand your point. Then again you might be able to use reflection to verify that your manually rolled implementation actually serializes all fields.
- theICEBeardk 6mo agoI mean a readable implementation of tuple with minimal overhead is a great case for me (went from around 1.6k lines to approximately 250 lines). I wrote an implementation including the normally difficult to implement tuple_cat based on c++26 within a few hours. My favorite thing is that I will get to remove and replace most of the cryptic template recursion stuff I have with "template for" and maybe a bit of reflection. Debugging the unrolled stuff will be a joy in comparison.