I have written a template-based factory system for a project I'm working on. These are the signatures for some of the function templates I have:
template <typename Interface, typename... Args>
void register_factory(identifier id, function<shared_ptr<Interface> (Args...)> factory);
template <typename Interface, typename... Args>
void unregister_factory(identifier id);
template <typename Interface, typename... Args>
shared_ptr<Interface> create(identifier id, Args... args);
As you see, I have to give all my function templates a typename... Args
argument, because I need to access the variables in which I store factory functions:
template <typename Interface, typename... Args>
struct factory_storage {
static map<identifier, function<shared_ptr<Interface> (Args...)> factories;
};
But logically, I should only need those for register_factory
and create
, everywhere else knowing Interface
should suffice (all factory functions for the same interface have the same signature). In fact, if I had used void*
instead of std::function
, I could have gotten rid of most occurrences of typename... Args
in my code.
Is there a way I could keep the type-safety of std::function
and also avoid some of the clutter. I've seen std::tuple
used for storing typename...
arguments, and those could be one possible solution to my problem, but they seem overly complicated and I'm hoping to be able to avoid them.