#include <concepts>
#include <iostream>
#include <optional>
#include <syncstream>
co_await mp_coro::async([&] { std::cout <<
"Result: " << res << std::endl; });
co_return res + 23;
}
const auto res = co_await foo();
std::cout << "Result of foo: " << res << std::endl;
}
std::osyncstream(std::cout) << "About to throw an exception\n";
throw std::runtime_error("Some error");
std::osyncstream(std::cout) << "This will never be printed\n";
return 42;
});
std::osyncstream(std::cout) << "I will never tell you that the result is: " << res << std::endl;
co_return res;
}
co_await bar();
co_await boo();
}
template <typename T>
try {
if constexpr (std::is_void_v<T>)
else
} catch (const std::exception &ex) {
std::cout << "Exception caught: " << ex.what() << "\n";
}
}
int main() {
try {
test(example());
test(boo());
} 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,...