#include <iostream>
std::cout << "foo(): about to return\n";
co_return 42;
}
auto result = foo();
std::cout << "bar(): about to co_await\n";
const int i = co_await result;
std::cout << "i = " << i << '\n';
std::cout << "bar(): about to return\n";
co_return i + 23;
}
int val = 123;
std::cout << co_await foo() << '\n';
std::cout << co_await ref() << '\n';
}
std::cout << "empty\n";
co_return;
}
auto task = foo();
std::cout << "Result #1: " << co_await task << '\n';
std::cout << "Result #2: " << co_await task << '\n';
std::cout << "Result #3: " << co_await std::move(task) << '\n';
}
int main() {
try {
auto task = bar();
} catch (const std::exception &ex) {
std::cout << "Unhandled exception: " << ex.what() << '\n';
}
}
Task that produces a value of type T: to get that value, simply await the task.
decltype(auto) sync_await(A &&awaitable)
Creates a synchronized task from the awaitable, starts it, and waits for it to complete,...