It seems this can be done quite straight forward:
template <typename T>
std::shared_ptr<Base> make() {
return std::make_shared<T>();
}
template <typename... T>
class Factory {
public:
std::shared_ptr<Base> operator()(int index) {
static constexpr std::shared_ptr<Base> (*factories[])() = {
&make<T>...
};
if (index < 0 && sizeof...(T) <= index) {
throw std::range_error("type index out of range");
}
return (factories[index])();
}
};
I'm currently not in the position to compile the code but something along this lines should work: the idea is to create an array of factory functions and just call into this array.