Skip to content

Commit

Permalink
Add simple for-loop example to any_sender documentation example
Browse files Browse the repository at this point in the history
  • Loading branch information
msimberg committed Dec 6, 2024
1 parent 17342ef commit e1dbe71
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions examples/documentation/any_sender_documentation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,20 @@ int main(int argc, char* argv[])
fmt::print("Caught exception: {}\n", e.what());
}

// We can also use a type-erased sender to chain work. The type of the
// sender remains the same each iteration thanks to the type-erasure, but
// the work it represents grows.
//
// However, note that using a specialized algorithm like repeat_n from
// stdexec may be more efficient.
ex::unique_any_sender<int> chain{ex::just(0)};
for (std::size_t i = 0; i < 42; ++i)
{
chain = std::move(chain) | ex::continues_on(sched) |
ex::then([](int x) { return x + 1; });
}
print_answer("Final answer", std::move(chain));

pika::finalize();
pika::stop();

Expand Down

0 comments on commit e1dbe71

Please sign in to comment.