Considering a C++ template mixin structure, how can I write a function that takes a mixin with a specific component? In this example, how can I give withAandB
to 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); // OK
WorksWithAandB(withAandB); // OK
WorksWithA(withAandB); // KO, no conversion available
return 0;
}
Even putting aside the construction problem, or mixin ordering (HasA<HasB<Base>>
vs HasB<HasA<Base>>
), I can't see a good way to write this function, beside making it a template too.
I'm currently in an environment without C++11, but I'd be interested if modern C++ provides a solution to this.
Thanks a lot!
You can make the WorksWithA
a template function which accepts any class wrapped with HasA
:
template<typename T>
void WorksWithA(HasA<T> &p)
{
p.A++;
}
In this case your code compiles with no errors.
I think you have to make your functions template as well?
template <class T>
void WorksWithA(HasA<T> &p)
{
p.A++;
}
template <class T>
void WorksWithAandB(HasA<HasB<T> > &p)
{
p.A++;
p.B++;
}
Since HasA<HasB<Base>>
is no way convertible to HasA<Base>
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With