CppCon 2022: C++20's Coroutines for Beginners

2023/07/06
This article was written by an AI 🤖. The original article can be found here. If you want to learn more about how this works, check out our repo.

Andreas Fertig's talk at CppCon 2022 introduces developers to the new feature in C++20 called coroutines. This talk is perfect for those who are encountering this term for the first time. Fertig starts from the basics, explaining "normal" functions before diving into coroutines. He explores the various customization points that C++ offers, including the new keywords co_await, co_yield, and co_return. Fertig also discusses how to write a generator for a coroutine, as there is no STL part for that in C++20. Additionally, he explains the distinction between cooperative and preemptive multitasking and how coroutines eliminate the need for locks. By the end of the talk, developers will have a solid understanding of coroutines and where they can be used.

CppCon 2023: Registration Now Open Registration for CppCon 2023 is now open! The conference will take place in person from October 1st in Aurora, Colorado. To get a taste of what to expect, videos of top-rated talks from the previous year's conference are being shared. Don't miss out on this opportunity to enhance your C++ knowledge and network with fellow developers. Register today for CppCon 2023!

Upcoming C++ Conferences In addition to CppCon, there are several other upcoming C++ conferences that developers should keep an eye on. These conferences provide valuable opportunities to learn about the latest advancements in the C++ language and connect with industry experts. Here are a few notable conferences:

  • Italian C++ Conference: June 10 in Rome, Italy
  • C++ on Sea: June 27-30 in Folkestone, UK
  • CppNorth: July 17-19 in Toronto, Canada
  • CppIndiaCon: August 5, a virtual event

Stay tuned for more updates on these conferences and other important events in the C++ community.

// Example code demonstrating the use of coroutines in C++20
#include <iostream>
#include <experimental/coroutine>

struct generator {
    struct promise_type {
        int current_value;
        auto get_return_object() { return generator{this}; }
        auto initial_suspend() { return std::experimental::suspend_always{}; }
        auto final_suspend() { return std::experimental::suspend_always{}; }
        void return_void() {}
        void unhandled_exception() {}
        auto yield_value(int value) {
            current_value = value;
            return std::experimental::suspend_always{};
        }
    };
    
    generator(promise_type* p) : p(p) {}
    
    struct iterator {
        std::experimental::coroutine_handle<promise_type> h;
        bool operator!=(const iterator& rhs) const { return !h.done(); }
        void operator++() { h.resume(); }
        int operator*() const { return h.promise().current_value; }
    };
    
    iterator begin() {
        p->current_value = 0;
        return {std::experimental::coroutine_handle<promise_type>::from_promise(*p)};
    }
    
    iterator end() { return {nullptr}; }
    
    promise_type* p;
};

generator count_to_n(int n) {
    for (int i = 0; i < n; ++i) {
        co_yield i;
    }
}

int main() {
    for (int i : count_to_n(5)) {
        std::cout << i << ' ';
    }
    std::cout << '\n';
}

CppCon 2022 and the upcoming C++ conferences provide developers with valuable insights into the latest advancements in the C++ language. Whether it's learning about coroutines in C++20 or exploring other cutting-edge features, these conferences offer a platform for developers to expand their knowledge and network with industry experts. Stay tuned for more updates on the C++ community and make sure to register for CppCon 2023 to be a part of this exciting event.