Modernize code base with range-v3 library #3394
Replies: 2 comments
-
@VRichardJP Thank you for your proposal! FYI, I've discussed a similar proposal in the previous project Autoware.Auto: https://gitlab.com/autowarefoundation/autoware.auto/AutowareAuto/-/issues/1154 Since it's obvious that readability and quality will improve, I'm up for your suggestion! Also, it's ideal if there is a way to detect and warn old styles automatically. I guess ClangTidy might support it in the future as |
Beta Was this translation helpful? Give feedback.
-
To be fair, although I think ranges and views functional-style constructs are far superior to their usual imperative equivalent (improved readability, less error-prone, etc), using
I don't think any of these points is necessarily deal-breaking. Personally, I would always choose +1 minute compilation time over spending 3 days debugging a buffer overflow. Still, I think these are important points to keep in mind and monitor if we start to use ranges-v3. For instance, if someone makes a PR with ranges, we could ask to compare:
|
Beta Was this translation helpful? Give feedback.
-
C++20 and C++23 adds a lot of life changing features. Among others: views and ranges.
As every experienced C++ developer knows, the C way of iterating over a container is verbose, ugly and error-prone:
C++ once introduced iterator, but it did not make the code much better:
Hopefully, the problem was solved long ago with C++11 range-based for loop:
Unfortunatelly, this construct only works for simple iteration. For example in Autoware planning module, it is very common to access points by pair (the current point and the next). In such case we are back to the ugly and error-prone:
(Note: did you see the loop above reads past the last element?)
With C++23 (!!), such pattern would become very simple to implement:
Obviously, I don't think Autoware can switch to C++23 anytime soon. So is Autoware cursed using meaningless indices everywhere for another X years? Actually not, since this C++23 is heavily based on the range-v3 library. With the range-v3 library, the previous loop would look like:
This is just a simple example, there are a lot of common patterns that could be greatly simplified with just a few
v | views::XXX | actions::YYY
(a few examples).I see the range-v3 library a great way to modernize the code base, improve readability, avoid/fix many hard-to-detect out-of-bound access bugs and prepare a future transition to C++23. As far as I know, Autoware code base does not use range-v3, but it has dependencies on it, so the library is already available. Wherever you find yourself using indices, iterators or performing simple operation such as filtering, mapping and such, there is most likely a simpler range-based construct that can do that for you (and bug-free, and optimization friendly).
I know introducing a new library is always a big thing, but even if it is not to rewrite every for loop, I think Autoware would benefit greatly from using it at least for new code. (I am not sure whether there are any policy regarding library usage)
What do you think?
Beta Was this translation helpful? Give feedback.
All reactions