So i want to generate a lambda that will invoke a constructor by retrieving the constructor's parameters from a factory.
I want to convert this code that works right now into a variadic template so it will work with any number of constructor parameters.
Any idea how to do this?
template <class Class>
struct Constructor
{
};
template <class Class>
struct Constructor< Class() >{
static std::function<Class*()> Instanciator(Factory& factory)
{
return []{
return new Class();
};
}
};
template <class Class, class Arg1>
struct Constructor< Class(Arg1) >{
static std::function<Class*()> Instanciator(Factory& factory)
{
return []{
return new Class(factory.Get<Arg1>());
};
}
};
template <class Class, class Arg1, class Arg2>
struct Constructor< Class(Arg1, Arg2) >{
static std::function<Class*()> Instanciator(Factory& factory)
{
return []{
return new Class(factory.Get<Arg1>(), factory.Get<Arg2>());
};
}
};
now
Constructor< Foo(Args...) >
is a function object of typeFactory -> std::function<Foo*()>
, whereFactory
supports.Get<Args>()
for eachArgs
.Me, I'd also want to include the index in the
.Get<?>()
call, in case your class takes twoint
arguments or whatever.I removed
Instanciator
and thestatic
bit, because a class whose only purpose is to produce constructors doesn't need that noise. Making it an invokable stateless function object is more idiomatic.