To clarify, I tried to do something like this back in the 90's and it was _not_ worth the effort.
Using something like Google's Protocol Buffers or a similar system where you generate a standard serialization backend for all the plain old data types you want to send across the network is way less of a pain in the ass.
If you're concerned about the overhead, look at MsgPack or Cap'n Proto - plenty of ways to shave the yak out there without needing to roll your own.
You're talking about serialization, and I agree that using reflection for that is both an overkill and somewhat hacky (you still have to define the serialization format).
Reflection on the other hand is immensely useful if you want to, for example, automatically generate type-safe bindings for, say, Python or .NET or Java, from your C++ classes.
One of the authors of that paper, Matúš Chochlík, has been working on this for years. He has a pretty nice implementation that I've used successfully on a number of occasions, even though it requires a pre-compiler pass to generate the necessary data.
If I'm not mistaken, he has been working into getting the necessary data available from the compiler directly (which this paper would confirm).
In game development it tends to help solve some common problems.
Games need a lot of iteration, C++ compile times can easily get very long i.e. minutes. You might object that compile times should never be long: "oh this never happens if you do X or Y" but in reality every C++ gamedev shop I've worked at has had long compiles times. Templates and other things of that nature tend to bloat it.
Anyway so reflection helps with:
- Hot-reloading code
- Tweaking reflected values at runtime
- Exposing functions for scripting languages (without having to write lots of repetitive boiler plate code)
- Saving / Loading
(And networking but that's not something I've experimented with personally!)
Depending on how it's implemented, at least for games, it's worth it.
The first two reasons are development/debugging concerns, and the last two are somewhat hacky. Could you elaborate how reflection is the optimal solution for your last two examples?
I would say the optimal solution for the last 2 depends on the total requirements - aka there is no general optimal solution.
Both concerns are about having the need for boilerplate code (either for bindings or for serialization), which is quite repetitive to write and depends on the input types.
Now the possible solutions are from my perspective:
- Write that code by hand, which is quite tedious and error-prone.
- Use some code generator tooling (like protoc) for it. However that might require you to define your types in an IDL before going to code, which leads to an extra step in the development and build process. In my experience this works really well for big teams (because you also have an extra interface documentation against which all parties can work), but it might not be optimal for small teams and rapid iterations.
- Generate the code automatically from the type definitions in your program. Either during compile-time (which would require good compiler support for extracting the relevant details and using them for code generation) or during runtime by reflection. With reflection you are most likely getting the slowest code. But iteration times should be the fastest (no need to change IDL files, run codegen tools, etc.)