7 ms·
if your data's sequential, creating an iterator in C++ is as simple as returning a begin and end pointer, and will be optimized away by any level other than O0
by RicardoLuis0 3y ago
if your data's sequential, creating an iterator in C++ is as simple as returning a begin and end pointer, and will be optimized away by any level other than O0
https://godbolt.org/z/WEjzEr5j4 https://godbolt.org/z/WEjzEr5j4
- josefx 3y agoBut iterating over pointers is once again optimized with lots of undefined behavior at the corners. So you are replacing one source of undefined behavior with another.
- yazaddaruvala 3y agoPointer arithmetic optimization based on undefined behavior is a problem regardless. Life is always better after minimizing the total number of types of undefined behavior.
- AYoung010 3y agoReplacing undefined behavior at the program-level with undefined behavior written and tested as part of the standard library, usually vendored and distributed in concert with the compiler, seems like an obvious net-positive to me.
- commonlisp94 3y ago> with lots of undefined behavior at the corners. What behavior is undefined in incrementing a pointer between a begin and end range?
- josefx 3y agoOf course a basic iteration between begin()/end() will never contain out of range elements, but neither will valid increment between two integers. No need for iterators in that case either. Say I want to do something fancy, like getting element number 11 from an array. With an integer index I can pass 11, with random access iterators I can use begin() + 11. Now my array only has five elements. So I check. 11 < 5? Comparison valid, successfully avoided a crash. begin() + 11 < end() ? How where the rules for pointer comparison again, something about within an allocation and one past the end?
- commonlisp94 3y ago> something about within an allocation and one past the end? Yeah, I forgot about that. So I agree there is some subtly which is likely to catch beginners. Your example could safely be: if (std::distance(begin, end) > 5) Another approach I would recommend is to write a `guarded_advance` which takes an integer and the end pointer. Also note that the situation you are describing is still a little unusual because the baseline assumption is it takes linear time to advance an iterator by more than 1 increment. > but neither will valid increment between two integers. No need for iterators in that case either. The purpose of an iterator is to abstract data structure access. The coordinate inside a complex data structure may not be representable by an integer.