by alexvitkov · 3y ago · 0 comments
You don't trust your compiler to optimize the sane trivial C case, but you trust it to optimize all that garbage away?
Life is always better after minimizing the total number of types of undefined behavior.
What behavior is undefined in incrementing a pointer between a begin and end range?
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?
> but you trust it to optimize all that garbage away?
Yep, if you learn about compilers you learn what kind of optimizations are easy and hard for them to make. The kind that just flattens a few levels of nested definitions are easier.