The definition of pointer arithmetic from the C++ Standard, [expr.add], paragraph 7, states 7 [ISO/IEC 14882-2014], states:
For addition or subtraction, if the expressions
P
orQ
have type “pointer to cvT
”, whereT
is different from the cv-unqualified array element type, the behavior is undefined. [Note: In particular, a pointer to a base class cannot be used for pointer arithmetic when the array contains objects of a derived class type. —end note]
...
The elements in the arrays are no longer polymorphic objects (instead, they are pointers to polymorphic objects), and so there is no no undefined behavior with the pointer arithmetic.
...
Another approach is to use a standard template template library (STL) library container instead of an array and have f()
accept iterators as parameters, as in this compliant solution. However, because STL containers require homogeneous elements, pointers are still required within the container.
...
[ISO/IEC 14882-2014] | 5.7, "Additive Operators" |
[Stroustrup 06] | "What's wrong Wrong with arraysArrays?" |
[Meyers 06] | Item 3: , Never treat arrays polymorphically |
[Lockheed Martin 05] | AV Rule 96, Arrays shall not be treated polymorphically |
[Sutter 04] | Item 100: , Don't treat arrays polymorphically |
...