0

Suppose I have:

void f()
{
    SomeClass someObject;
    pplx::create_task([&]()-> SomeClass { return someObject; });
}

void g()
{
    //Allocate automatic objects
}

int main()
{
    f();
    g();
}

Is this guaranteed to work? my logic says no since by the time the task runs someObject might be out of scope.

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
Shmoopy
  • 5,334
  • 4
  • 36
  • 72

1 Answers1

1

It depends. If the lambda (or a copy thereof) is never used after f() returns, everything is fine. Afterwards of course return someObject is evaluating a reference to an object that no longer exists, which is undefined behaviour.

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084