Are there any built-in C++ mechanics that provide an ability to automatically generate code to call a function or operator on every member of a class?
For example, if we have hundreds of types that need to employ a specific set of functions, or operator. And within each one, each type needs to call that same function or operator for all of its members (and that's it).
For some specific examples, we could say the types have functions Save() and Load(), or stream operators << and >>. How can we automatically execute A.Save(s) + B.Save(s) + C.Save(s) when TYPE::Save(stream s) is called? If the code simply passes execution to all members identically, is there any way to automate the process?
Here is an example of some code that would be automatically generated (just the Save() function). EDIT NOTE: This example shows all 3 members as the same type, but the solution should work on members of any combination of types, as long as they all share the related function/operator
class TYPE
{
protected:
XTYPE A, B, C;
public:
void Save(stream s)
{
A.Save( s );
B.Save( s );
C.Save( s );
}
};
One of my older engines used crazy looping macros to pull this off. It makes it much easier to build types using the macros (when there are hundreds of classes that use them), and the resulting classes are not difficult to understand, but the macros themselves are messy and overly complicated. I was hoping for something more.. built into the language.