Lightning Talk: operator for - C++ Generator Ranges Without Coroutine Overhead - Jonathan Müller
Вставка
- Опубліковано 5 вер 2024
- www.cppnow.org
/ cppnow
---
Lightning Talk: operator for - C++ Generator Ranges Without Coroutine Overhead - Jonathan Müller - CppNow 2023
Slides: github.com/boo...
---
If you want to support the range-based for loop for your type, you have to write an iterator for it. Depending on the complexity of the range, this can be really complicated, since you need to awkwardly model a state machine. Luckily, since C++23 we have std::generator, which gives us a way of using coroutines to specify the range - just write a loop and co_yield each value. However, coroutines have significant overhead, may introduce heap allocation, and are in general more difficult for the optimizer to reason about.
A much simpler solution is to write a function that takes a lambda, which is then invoked for each element. This is fast, but a bit awkward since you cannot use the range-based for loop. This talk discusses `operator for`, an upcoming C++ proposal that adds language support for this pattern.
---
Jonathan Müller
Jonathan is a library developer at think-cell. In his spare time, he works on various C++ open source libraries for memory allocation, cache-friendly containers, or parsing. He also blogs at foonathan.net and is a member of the C++ standardization committee.
---
Video Sponsors: think-cell and Bloomberg Engineering
Audience Audio Sponsors: Innoplex and Maryland Research Institute
---
Videos Filmed & Edited By Bash Films: bashfilms.com/
UA-cam Channel Managed & Optimized By Digital Medium Ltd: events.digital...
---
CppNow 2024
www.cppnow.org
/ cppnow
---
#boost #cpp #cppnow
at 2:54 I presume [=] capture is terrible for performance?
Ok but... why... Seems a bit over the top.
Nicer code without coroutine overhead
Either you have many places in your code that loops over exactly, and exactly, 3 sequences, and you want to simplify the code, or you could make a variasic template with this and make it generic