https://github.com/avakar/corecursive
Single-header C++20 library to remove recursion using coroutines
https://github.com/avakar/corecursive
Last synced: about 2 months ago
JSON representation
Single-header C++20 library to remove recursion using coroutines
- Host: GitHub
- URL: https://github.com/avakar/corecursive
- Owner: avakar
- License: 0bsd
- Created: 2020-04-17T16:19:36.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2020-04-17T16:19:46.000Z (about 5 years ago)
- Last Synced: 2025-03-22T15:02:22.816Z (2 months ago)
- Language: C++
- Homepage:
- Size: 4.88 KB
- Stars: 12
- Watchers: 3
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# corecursive
Single-header C++20 library to remove recursion using coroutines.
## Motivation
Recursive functions are often an easy way to implement an algorithm,
but you must be careful not to overflow your execution stack.
This is especially important if the input to your recursive function
comes from an untrusted source.Typically, you avoid stack overflows either by
* limiting the depth of the recursion and
failing the algorithm at some arbitrary depth, or
* manually rewriting the function logic into a state machine.Now with `co_await`, there is a third option.
## Getting Started
Consider the canonical awful factorial implementation.
int factorial(int n) {
if (n <= 1)
return 1;
return n * factorial(n - 1);
}
assert(factorial(5) == 120);To make the function iterative, change its return type to `corecursive`
and add `co_await` in front of all recursive calls.#include
using avakar::corecursive;corecursive factorial(int n) {
if (n <= 1)
co_return 1;
co_return n * co_await factorial(n - 1);
}
assert(factorial(5) == 120);That's it, your function is no longer recursive and stores its state on the heap.
Read more in the [documentation](docs/index.md).## License
The library is licensed under the [zero-clause BSD license](LICENSE),
which is almost the same as public domain and is compatible with everything.