I have something like that based on variadic templates:
class MyClass {
public:
template <typename... OtherTs>
typename std::enable_if<sizeof...(OtherTs) == 0>::type Setup() {}
template <typename T, typename...OtherTs>
void Setup() {
DoSomeStuff(new T());
Setup<OtherTs>();
}
};
It's working well and I can setup an instantiation of MyClass
based on other classes like that myClass.Setup<A,B,C>()
.
What I would like to do now is to define subpackages of classes like Default = A, B
such that myClass.Setup<Default, C>()
or even myClass.Setup<MyClass::Default, C>()
will be interpreted as myClass.Setup<A,B,C>()
.
I know I can define #define Default A, B
but is there a similar way that does not rely on macro and could be defined in a namespace/class?
Thanks for reading and have a nice day. :)
You might use recursion when tuple or any specific type (tag
here) is given:
template <typename...> struct tag{};
class MyClass {
private:
template <typename T>
void SetupImpl(tag<T>)
{
// Here, individual setup
DoSomeStuff(new T());
}
template <typename ...Ts>
void SetupImpl(tag<tag<Ts...>>)
{
Setup<Ts...>();
}
public:
template <typename ... Ts>
void Setup() {
(SetupImpl(tag<Ts>{}), ...); // C++17 fold expression
}
};
using Default = tag<A, B>;