C++20 Coroutines back to basics — Restrictions and rules for legal coroutines
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:
- What are the restrictions on coroutines ?
- 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
orConcept
). Constexpr functions, constructors, destructors, and the main function cannot be coroutines.