Real-World Example 2
Explore how to add reflection capabilities in C++ by creating a reflect() function that returns members as a tuple. Understand how this enables generic operations like serialization and logging, reducing boilerplate code. Learn to use concepts for conditional function overloading to automate stream output for reflectable classes.
Reflection
The term reflection refers to the ability to inspect a class without knowing anything about its contents. In contrast to many other programming languages, C++ does not have built-in reflection, which means we have to write the reflection functionality ourselves. Reflection will be included in future versions of the C++ standard; hopefully, we will see this feature in C++23.
In this example, we are going to limit the reflection to give classes the ability to iterate their members, just like we can iterate the members of a tuple. By using reflection, we can create generic functions for serialization or logging that automatically work with any class. This reduces large amounts of boilerplate code, which is traditionally required for classes in C++.
Making a class reflect its members
Since we need to implement all the reflection functionality ourselves, we will start by exposing the member variables via a function called ...