C++ co_return Keyword
The co_return
keyword in C++ was introduced in C++20 as part of the coroutine framework. It is used inside coroutines to return a value or signal completion to the coroutine’s caller. Unlike a regular return
statement, co_return
interacts with the coroutine’s promise object and manages the coroutine’s lifecycle.
Coroutines, combined with co_return
, co_await
, and co_yield
, provide a robust framework for asynchronous programming, lazy evaluation, and more.
Syntax
</>
Copy
co_return expression;
- co_return
- The keyword used to return a value or signal completion from a coroutine.
- expression
- The value to be returned to the coroutine’s caller.
Examples
Example 1: Simple Coroutine with co_return
This example demonstrates a coroutine that uses co_return
to return a value to the caller.
</>
Copy
#include <iostream>
#include <coroutine>
#include <memory>
// Coroutine return type
struct Generator {
struct promise_type {
int value;
Generator get_return_object() { return Generator{std::coroutine_handle<promise_type>::from_promise(*this)}; }
std::suspend_always initial_suspend() { return {}; }
std::suspend_always final_suspend() noexcept { return {}; }
void unhandled_exception() { std::exit(1); }
void return_value(int v) { value = v; }
};
std::coroutine_handle<promise_type> handle;
Generator(std::coroutine_handle<promise_type> h) : handle(h) {}
~Generator() { handle.destroy(); }
int get() { return handle.promise().value; }
};
Generator generateValue() {
co_return 42;
}
int main() {
auto generator = generateValue();
std::cout << "Generated Value: " << generator.get() << std::endl;
return 0;
}
Output:
Generated Value: 42
Explanation:
- The coroutine
generateValue
usesco_return
to return the value42
to the caller. - The custom promise type provides a
return_value
method to store the returned value. - The caller retrieves the returned value using the
get()
method of theGenerator
object.
Example 2: Coroutine Returning a std::future
This example shows how to use co_return
with a coroutine that returns a std::future
.
</>
Copy
#include <iostream>
#include <future>
#include <coroutine>
// Coroutine function returning a future
std::future<int> computeValue() {
co_return 100;
}
int main() {
auto fut = computeValue();
std::cout << "Computed Value: " << fut.get() << std::endl;
return 0;
}
Output:
Computed Value: 100
Explanation:
- The coroutine
computeValue
usesco_return
to return the value100
. - The returned value is wrapped in a
std::future
, allowing the caller to retrieve it asynchronously. - The
fut.get()
call blocks until the coroutine completes and the value is ready.
Key Points about co_return
Keyword
- The
co_return
keyword is used to return a value from a coroutine. - It interacts with the coroutine’s promise object, enabling efficient communication between the coroutine and its caller.
- Coroutines with
co_return
can return custom types, such asstd::future
, or user-defined types with promise objects. co_return
provides a way to signal the completion of a coroutine and return a result.- Introduced in C++20, coroutines require a C++20-compatible compiler and the
-std=c++20
flag enabled during compilation.