| ▲ | bluGill 2 hours ago |
| 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. |
|
| ▲ | maccard 2 hours ago | parent | next [-] |
| Serialization 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. |
| |
| ▲ | SuperV1234 an hour ago | parent | next [-] | | You can achieve that since C++20 (or C++17 if you don't care about the member names). E.g. https://www.linkedin.com/posts/vittorioromeo_cpp-gamedev-ref... (The link above shows ImGui generation, but the same exact logic can be applied for serialiation to JSON/YAML/whatever.) | | |
| ▲ | maccard 13 minutes ago | parent [-] | | Sure, but > The magic sauce? Boost.PFR! An incredibly clever library that enables reflections on aggregates, even in C++17. That's not vanilla C++! | | |
| |
| ▲ | bluGill an hour ago | parent | prev [-] | | It 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. | | |
| ▲ | maccard 9 minutes ago | parent | next [-] | | I disagree. Rust's defacto default is serde, golang comes with batteries included, dotnet/java have had it for _years_, and all the dynamic languages do it. | |
| ▲ | electroly an hour ago | parent | prev | next [-] | | Can you give an example of a language ecosystem that went with reflection-based JSON serialization/deserialization and then went on to regret it? I can't think of any, and don't agree with your conclusion. It works great, and manually writing serialization and matching deserialization code is terrible, annoying, error-prone work. | |
| ▲ | SuperV1234 an hour ago | parent | prev [-] | | I 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. |
|
|
|
| ▲ | cogman10 2 hours ago | parent | prev | next [-] |
| It 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). |
| |
| ▲ | SuperV1234 an hour ago | parent | next [-] | | Java 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#. | |
| ▲ | david422 an hour ago | parent | prev [-] | | > 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. | | |
| ▲ | cogman10 an hour ago | parent | next [-] | | Initially, 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. | |
| ▲ | kuboble an hour ago | parent | prev [-] | | Reflection is simply a syntax vinegar for duck typing. |
|
|
|
| ▲ | surajrmal 2 hours ago | parent | prev [-] |
| Anybody the derive traits rust has are a good demo. |