https://gcc.gnu.org/bugzilla/show_bug.cgi?id=108517
--- Comment #4 from Jonathan Wakely <redi at gcc dot gnu.org> --- Libstdc++ isn't causing it, but we can give the compiler enough information to make it shut up: --- a/libstdc++-v3/include/bits/stl_algo.h +++ b/libstdc++-v3/include/bits/stl_algo.h @@ -1814,6 +1814,10 @@ _GLIBCXX_END_INLINE_ABI_NAMESPACE(_V2) { if (__first == __last) return; + if (__first == _RandomAccessIterator() + || __last == _RandomAccessIterator()) + __builtin_unreachable(); + for (_RandomAccessIterator __i = __first + 1; __i != __last; ++__i) { if (__comp(__i, __first)) As you can see there, we already return early and never reach line 1819 where the dereference happens. We can tell the compiler that it's impossible to get to that that dereference with a value-initialized iterator, because if one is null the other must be, and so we'd already have returned.