I want to do something similar to the code below, except that I don't want to implement func()
twice because it will be the same implementation. Do you have any proposal how to accomplish this?
template <typename First, typename... Rest>
class Base : public Base<Rest...> {
public:
using Base<Rest...>::func;
void func(First object) {
// implementation
}
};
template <typename First>
class Base<First> {
public:
void func(First object) {
// implementation
}
};
struct X {};
struct Y {};
struct Z {};
class Derived : public Base<X, Y, Z> {};
// ...
Derived d;
d.func(X{});
d.func(Y{});
d.func(Z{});
With multiple inheritance and an additional using
directive:
template <typename First, typename... Rest>
struct Base : Base<First>, Base<Rest...> {
using Base<First>::func;
using Base<Rest...>::func;
};
template <typename First>
struct Base<First> {
void func(First object) {/*...*/}
};
Could be this a solution? (Move definition of func to a common base?)
template <class First>
class Func_impl
{
public:
void func(First obj) {
// implementation
}
}
template <typename First, typename... Rest>
class Base : public Base<Rest...>, Func_impl<First> {
public:
// no need to declare func here
};
template <typename First>
class Base<First> : public Func_impl<First> {
public:
// no need to declare func here
};
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