r/cpp • • Jan 02 '26

Every LLM hallucinates that std::vector deletes elements in a LIFO order

249 Upvotes

118 comments sorted by

View all comments

Show parent comment

15

u/mark_99 Jan 02 '26 edited Jan 02 '26

You (and the LLM) are quite correct here to highlight what do they even mean by "LIFO order" given you can insert anywhere in a vector.

This might be one reason why the standard doesn't specify it, as it might coincidently work of you only do push_back only to be caught out when inserting elsewhere.

Some people are keen to latch on to "LLM gets it wrong" posts, but if you can't even formulate your question correctly it's not a good start.

-2

u/The_JSQuareD Jan 02 '26

IMO the standard should make some promise about the order of destruction to ensure consistent behavior between implementations. Or even to ensure consistent behavior run-to-run (it seems that under the current wording, a conforming implementation could use a non-deterministic RNG to pick the destruction order). Guaranteeing LIFO behavior would obviously not be practical since it would come with performance penalties. But guaranteeing that the destruction order is back-to-front, consistent with arrays (both static and dynamically allocated), seems entirely reasonable and desirable. Similar but different guarantees could be made for other containers (depending on what order(s) of traversal / deletion are efficient).