多次调用时,`std::future::then` 的行为是什么?
What is the behaviour of `std::future::then` when called more than once?
根据Concurrency TS,下面的代码会发生什么?
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;});
第三行代码执行的时候,f0
已经有了continuation,所以,根据TS,应该f0
抛出异常,中止程序,UB还是有不同的行为?我不清楚。
根据cppreference,未定义:
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
.
根据Concurrency TS,下面的代码会发生什么?
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;});
第三行代码执行的时候,f0
已经有了continuation,所以,根据TS,应该f0
抛出异常,中止程序,UB还是有不同的行为?我不清楚。
根据cppreference,未定义:
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()
isfalse
.