The function-call operator of the lambda is const-qualified by default, so foo_o1
that was copy-captured is non-modifiable inside of it.
You can mark the lambda as mutable
:
mutable
: allows body to modify the objects captured by copy, and to call their non-const member functions
E.g.
auto test_foo = [foo_o1 = make_unique<Foo>(), &vectorOfFoo]() mutable {
auto foo_o2 = make_unique<Foo>();
vectorOfFoo.push_back(std::move(foo_o2)); //COMPILES
vectorOfFoo.push_back(std::move(foo_o1)); //COMPILES
};
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…