For a policy-based class-design I need some of the policies to call functions that are found in other policies:
struct PolicyA {
void foo() {
// ...
}
};
struct PolicyB {
void bar() {
// Need to call foo() here
}
};
template <class A, class B> MyClass : A, B {};
The two options I see are:
- Pass
MyClass<A, B>
toPolicyA
andPolicyB
as template parameters and do something likedynamic_cast<MyClass<A, B>*>(this)->foo();
. However this may easily get very complicated (especially if it's not onlyB
callingA
but also the other way). - Have a base class for each policy where all the functions are declared as pure virtual. This fails when one of the function needs its own template parameters, since virtual template functions are not possible.
This should be a common problem, but I didn't find anything. I guess there probably is some boost-magic or something. How would you solve this?
Edit: See also the followup here.