Namespaces
Variants

std::experimental::shared_future<T>:: then

From cppreference.net

template < class F >
future < /* see below */ > then ( F && func ) const ;

将延续函数 func 附加到 * this 。若 * this 未关联共享状态(即 valid ( ) == false )则行为未定义。

创建与待返回的 future 对象关联的共享状态,并构造 func 的副本 fd ,其构造方式如同在调用 then 的线程中执行 DECAY_COPY ( std:: forward < F > ( func ) ) 表达式,其中 DECAY_COPY 定义为

template<class T>
std::decay_t<T> DECAY_COPY(T&& v)
{
    return std::forward<T>(v);
}

当当前与 * this 关联的共享状态就绪时,将在未指定的执行线程上调用 continuation INVOKE ( std :: move ( fd ) , * this ) ,其中 INVOKE 是在 Callable 中定义的操作。若该表达式无效,则行为未定义。

从延续操作返回的任何值将作为结果存储于返回的 future 对象的共享状态中。从延续执行过程中传播的任何异常将作为异常结果存储于返回的 future 对象的共享状态中。

U 为延续函数的返回类型(即 std:: result_of_t < std:: decay_t < F > ( const std:: experimental :: shared_future < T > & ) > )。若 U 是某个类型 T2 std:: experimental :: future < T2 > ,则 then 的返回类型为 std:: experimental :: future < T2 > ,否则为 std:: experimental :: future < U > 。此特性称为 隐式解包

如果发生隐式展开且 continuation 返回无效的 future ,则共享状态会以类型为 std::future_error 的异常准备就绪,其错误条件为 std::future_errc::broken_promise

此函数返回后, valid ( ) true

参数

func - 要附加的延续函数

返回值

一个与此对象创建的共享状态相关联的 std::experimental::future 对象。返回对象的 valid ( ) == true

示例