Coroutines – A Deep Dive -- Quasar Chunawala

logo.pngCoroutines are powerful but require some boilerplate code. Quasar Chunawala explains what you need to implement to get coroutines working.

Coroutines – A Deep Dive

by Quasar Chunawala

From the article:

The following code is the simplest implementation of a coroutine:

  #include <coroutine>
  void coro_func(){
    co_return;
  }
  int main(){
    coro_func();
  }

Our first coroutine will just return nothing. It will not do anything else. Sadly, the preceding code is too simple for a functional coroutine and it will not compile. When compiling with gcc 15.2, we get the error shown in Figure 1.

<source>: In function 'void coro_func()':
<source>:4:5: error: unable to find the promise type for this coroutine
    4 |     co_return;
      |     ^~~~~~~~~
Figure 1

Looking at C++ reference, we see that the return type of a coroutine must define a type named promise_type.

Add a Comment

Comments are closed.

Comments (0)

There are currently no comments on this entry.