This seems surprising and problematic. Admittedly, in most cases the order of destruction won't matter, but in some cases it certainly might. Getting inconsistent behavior between implementations here seems like an unnecessary self-inflicted wound. As far as I can tell, there would be no performance penalty for the standard to simply prescribe an order. For std::vector going from back to front seems reasonable (and consistent with arrays). For other containers I think you can also prescribe an order (not necessarily matching the vector's destruction order) that won't incur a performance penalty; e.g., for std::forward_list you'd prescribe that the order of destruction is the forward order in the list.
Or to give you a less glib answer: the standard defines the order destructors run in when you call delete[]. It is reasonable to expect that it would also define it for the most common library wrapper around a dynamic array.
I'm willing to bet (only a very very small amount of Money) that different compilers already implement this inconsistently and this it can never be fixed due to backwards compatibility concerns.
View all comments
Show parent comment
1
u/The_JSQuareD Jan 02 '26
This seems surprising and problematic. Admittedly, in most cases the order of destruction won't matter, but in some cases it certainly might. Getting inconsistent behavior between implementations here seems like an unnecessary self-inflicted wound. As far as I can tell, there would be no performance penalty for the standard to simply prescribe an order. For
std::vectorgoing from back to front seems reasonable (and consistent with arrays). For other containers I think you can also prescribe an order (not necessarily matching the vector's destruction order) that won't incur a performance penalty; e.g., forstd::forward_listyou'd prescribe that the order of destruction is the forward order in the list.