I'm pretty new with c++ and am currently writing an inversion of control container for my first project, expanding on this blog post by adding registering on base classes and forwarding additional arguments to the constructor.
It works pretty good for now but when I instantiate a lambda multiple times the captured values seem to get overridden.
Example:
struct A{
short a;
explicit A(short a_) : a(a_) {}
};
struct IC{
virtual unsigned C() = 0;
};
struct CImpl : public IC{
explicit CImpl(unsigned c_) : IC(), c(c_) {}
unsigned C() override{return c;}
private:
unsigned c;
};
template<class T, typename...TArgs>
std::function<T*()> AsMinimalAsItGets(TArgs&&...args)
{
return [&args...]() mutable -> T*
{
return new T(std::forward<TArgs>(args)...);
};
}
auto aFactory = AsMinimalAsItGets<A>(3);
auto cFactory = AsMinimalAsItGets<CImpl>(5);
auto aInst = aFactory();//aInst->a should be 3 but is 5
auto cInst = cFactory();//cInst->C() is 5
A gets instantiated with 5 instead of 3.
I tried this as solution, but it didn't fix the problem.
So how do I correctly capture the variables when instantiating the lambda? I need to capture in a way that enables me to use perfect forwarding in the lambda