Bartosz Milewski Wrote: > Steven Schveighoffer Wrote: > > > As long as the spec says changing length may expand the array to hold > > enough space, the optimizer can't, because the optimization would change > > the side effects of the function. An optimizer should not change the > > outcome or side effects of a function. It's not unheard of for an > > optimizer to eliminate important operations that it thinks are invalid, but > > in that case, it's a broken optimizer, I don't see why we would add this > > case. > > This is all true for user-defined data types, but array is totally under > compiler's control. What optimizations are acceptable depends only on the > interpretation of the language spec. In particular the sequence > > a.length -= 1; > a.length += 1; > > as a whole does not result in stomping, so why would it be wrong to optimize > it away? I claim it's perfectly legitimate.
In fact, adding length modifies the contents. For example: auto str = "hello"; str.length -= 1; str.length += 1; assert(str == "hell\xFF"); If the length modifications are optimized out, the assert fails. Looks like an invalid optimization to me. -Steve