What is the behaviour of `std::future::then` when called more than once?

610 views Asked by At

According to the Concurrency TS, what should happen in the following code?

auto f0 = std::async([]{return 0;});
auto f1 = f0.then([](auto& f){ return f.get() + 10; });
auto f2 = f0.then([](auto& f){ if(!f.valid()) return; return f.get() + 10;});

By the time the third line of code is executed, f0 already has a continuation, so, according to the TS, should f0 throw an exception, abort the program, UB or has a different behaviour? It's unclear to me.

1

There are 1 answers

3
clcto On BEST ANSWER

According to cppreference, it is undefined:

Attach the continuation func to *this. The behavior is undefined if *this has no associated shared state (i.e., valid() == false).

...

After this function returns, valid() is false.