c++ - Is it safe to use iterator pointing beyond indexable collection in calculations? -
when have simple pointer can safely wrap array this:
p+=x; // p pointer p can point beyond end of array if(p>=array+array_size) p-=array_size; // forget potential multiple wrap can same safely iterator on indexable container, esp. deque?:
p+=x; if(p>=mydeque.end()) p-=mydeque.size(); // forget potential multiple wrap in other words: safe take calculations iterator pointing beyond collection?
you can p += x; long still points within array/container or 1 past end of array/container (§5.7):
if both pointer operand , result point elements of same array object, or 1 past last element of array object, evaluation shall not produce overflow; otherwise, behavior undefined.
the same true comparisons between pointers. must both pointing @ same array or 1 past end. cannot pointing further that.
Comments
Post a Comment