https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107367
--- Comment #5 from Jonathan Wakely <redi at gcc dot gnu.org> --- This optimization isn't valid if any iterator operations can throw, because such an exception should terminate the loop early. Performing the algo on raw pointers would alter the behaviour.