I want to iterate over types of a tuple, not its elements.
Imagine you have a generic, base-class interface Controller
and you want to have a vector of pointers (I use raw pointer instead of smart, for readability): std::vector<Controller*> ctrls;
Now you want to add many implementations of Controller
interface to this vector, so you can do this:
ctrls.emplace_back(new MouseCtrl());
ctrls.emplace_back(new KeyboardCtrl());
ctrls.emplace_back(new ScreenCtrl());
(... and so on)
But this is ugly and not exactly extendable (as in Open-Close Principle), so it would be better to have, for example, a tuple: using Controllers = std::tuple<MouseCtrl, KeyboardCtrl, ScreenCtrl>;
and then, in the some init function, iterate over those types:
for (T : Controllers> {
ctrls.emplace_back(new T());
}
Obviously, the code above it's not valid C++ syntax. So the question is: how to do this?. I've looked both into std::apply and std::visit / std::variant, but I don't have idea how to do this (they iterate over elements, not types).