template <typename F> 
auto then(F&& f) const&
(1)
template <typename E, typename F> 
auto then(E&& e, F&& f) const&
(2)
template <typename F>
auto then(F&& f) &&
(3)
template <typename E, typename F>
auto then(E&& e, F&& f) &&
(4)

Creates a new continuation on the current object.

Multiple then() invocations on the same future object realize a split. The result of this will be copied into all continuations.

If T of future<T> is a move only type, then a split is not possible of course.

  1. Constructs a continuation with the passed callable object using the same executor as this.

  2. Constructs a continuation with the passed callable object using the provided executor.

  3. Constructs a continuation on a r-value with the passed callable object using the same executor as this.

  4. Constructs a continuation on a r-value with the passed callable object using the provided executor.

Parameters

e

Executor which is used to schedule the resulting task

f

Callable object that implements the continuation. T of this must be convertible to the parameter of f.

Return value

a future of the type of the result type of the passed function object.

Example 1

#include <cstdio>
#include <thread>
#include <stlab/concurrency/default_executor.hpp>
#include <stlab/concurrency/future.hpp>

using namespace std;
using namespace stlab;

int main() {
    auto x = async(default_executor, [] { return 42; });

    auto y = x.then([](int x) { printf("Result %d \n", x); });

    // Waiting just for illustrational purpose
    while (!y.get_try()) { this_thread::sleep_for(chrono::milliseconds(1)); }
}

// Result: Result 42

Example 2

#include <cstdio>
#include <thread>

#include <stlab/concurrency/default_executor.hpp>
#include <stlab/concurrency/future.hpp>

using namespace std;
using namespace stlab;

int main() {
    auto x = async(default_executor, [] { return 42; });

    auto c1 = x.then([](int x) { printf("Split A %d \n", x); });
    auto c2 = x.then([](int x) { printf("Split B %d \n", x); });

    // Waiting just for illustrational purpose
    while (!c1.get_try()) { this_thread::sleep_for(chrono::milliseconds(1)); }
    while (!c2.get_try()) { this_thread::sleep_for(chrono::milliseconds(1)); }
}
/*
    Result: (Note: The order of the results is not defined.)

        Split B 42
        Split A 42
*/