Given the mixin structure of a C ++ template, how can I write a function that accepts mixin with a specific component? In this example, how can I pass withAandBin worksWithA()?
struct Base {};
template <class T>
struct HasA : T
{
int A;
};
template <class T>
struct HasB : T
{
int B;
};
void WorksWithA(HasA<Base> &p)
{
p.A++;
}
void WorksWithAandB(HasA<HasB<Base> > &p)
{
p.A++;
p.B++;
}
int _tmain(int argc, _TCHAR *argv[])
{
HasA<Base> withA;
HasA<HasB<Base> > withAandB;
WorksWithA(withA);
WorksWithAandB(withAandB);
WorksWithA(withAandB);
return 0;
}
Even putting aside the problem of constructing or streamlining the mixing ( HasA<HasB<Base>>vs HasB<HasA<Base>>), I do not see a good way to write this function, in addition to making it also a template.
I am currently working in an environment without C ++ 11, but I would be interested if modern C ++ provided this solution.
Thank you so much!
source
share