I'm using an async function that takes an object reference &foo
and a callback cb
as arguments. I want to prevent destruction of foo
until the callback is called.
void async_thing(Foo &foo, function<void()> cb) {
// do something asynchronously with foo
}
Is it enough to simply capture it in the callback lambda? Or does it need to be actually used in the lambda?
auto foo = make_shared<Foo>();
async_thing(*foo, [foo]() {
cout << "Callback ran" << endl;
});
Might a compiler optimise the capture out, and delete foo
prematurely?
Aucun commentaire:
Enregistrer un commentaire