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 9, 2024
1 parent 17342ef commit d42f2f2
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions examples/documentation/any_sender_documentation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <fmt/printf.h>

#include <chrono>
#include <cstddef>
#include <string_view>
#include <thread>
#include <utility>
Expand Down Expand Up @@ -55,6 +56,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 d42f2f2

Please sign in to comment.