Summary
Automating the delegation of implementation of an interface to an expression.
Advertisement
For some time I have looked for a way to have automated delegation and dynamic inheritance in C++ without introducing extra pointers in a class.
Delegation is a technique of forwarding the implementation of a particular interface to a field or expression. Typically in C++ this is only done manually, i.e.:
The problem with this is that it is horribly tedious, and tedium always screams at me "there has got to be a better way". Well I have found a way to automate delegations, without introducing extra pointers into a class., I've described it briefly in this article at CodeProject.
For those casually interested, I did this so I can reduce the work performed by HeronFront the Heron to C++ translator. This technique lets me do some really cool stuff, without having to introduce type-checking and template expansion into the translator.
> The second parameter it is automatically deduced by the compiler, because it is passed as a function argument.
aha, yes. (i know the details of templates almost as badly as some the compilers. (maybe that's a good thing...))
now i am not so clear about the exact motivation/problem.
the basic case of delegation requires writing, eg.: void method1() { pObject_m->method1() } -- which is simpler than what you have. so you must be aiming at something else.
and it seems it is still necessary to write the ___Delegation structs and their methods (at least the signature parts).
so i feel there is some larger generalisation/problem not explained.
> the basic case of delegation requires writing, eg.: > void method1() { pObject_m->method1() } > -- which is simpler than what you have. so you must be > aiming at something else. > > so i feel there is some larger generalisation/problem not > explained.
Yes, I skipped the motivation, which was probably a bad idea.
The main motivating problem is "automated" delegation. Given a single large interface (with many function) which is delegated frequently, the technique outlined can be used to make delegation easier. You only have to define one delegation struct.
The other motivator is extension functions. Extension functions only operate on the interface. You can place them in a delegation struct, and get them for free in a delegating struct.
Finally there is the possibility to easily evaluate preconditions / postconditions within the delegation.
To be honest, the technique is hairy, but it is the best I can do in C++ to emulate features in Heron.