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:

  1. The coroutine generateValue uses co_return to return the value 42 to the caller.
  2. The custom promise type provides a return_value method to store the returned value.
  3. The caller retrieves the returned value using the get() method of the Generator 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:

  1. The coroutine computeValue uses co_return to return the value 100.
  2. The returned value is wrapped in a std::future, allowing the caller to retrieve it asynchronously.
  3. The fut.get() call blocks until the coroutine completes and the value is ready.

Key Points about co_return Keyword

  1. The co_return keyword is used to return a value from a coroutine.
  2. It interacts with the coroutine’s promise object, enabling efficient communication between the coroutine and its caller.
  3. Coroutines with co_return can return custom types, such as std::future, or user-defined types with promise objects.
  4. co_return provides a way to signal the completion of a coroutine and return a result.
  5. Introduced in C++20, coroutines require a C++20-compatible compiler and the -std=c++20 flag enabled during compilation.