C++20 Coroutines back to basics — Restrictions and rules for legal coroutines

Gajendra Gulgulia
5 min readMay 2, 2022

1. Introduction

In my previous articles on coroutines, I attempted to explain the mechanics of coroutine suspension, resuming it, promise_type, awaiters and awaitables. In this article, I would like to answer two very basic questions:

  1. What are the restrictions on coroutines ?
  2. When can a coroutine usage be legal in terms of member, non-member functions of class/structs, lambda expressions and so on ?

1.1 What this post is not about

While answering the above two questions, I will not dive into how the coroutines work or explain the compiler magic that happens when a coroutine is in execution. Nor will I explain about the lifetime of coroutines’ activation frame or the parameters passed to coroutines.

So without much ado lets jump right in.

2. Restrictions on C++20 Coroutines

Cpp reference page on coroutines says the following about restrictions on coroutines:

Coroutines cannot use variadic arguments, plain return statements, or placeholder return types (auto or Concept). Constexpr functions, constructors, destructors, and the main function cannot be coroutines.

--

--

Gajendra Gulgulia
Gajendra Gulgulia

Written by Gajendra Gulgulia

I'm a backend software developer and modern C++ junkie. I run a modern cpp youtube channel (https://www.youtube.com/@masteringmoderncppfeatures6302/videos)

No responses yet