r/cpp • • Jan 02 '26

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

251 Upvotes

118 comments sorted by

View all comments

Show parent comment

-6

u/am17an Jan 02 '26

Please share your chats, here is mine asking a similar question without any additional specifications (idk why you added those?), here is the simple prompt

"Can you tell me a container in C++ which would delete elements in an LIFO order on destruction?"

https://gemini.google.com/share/8be373c72e09

4

u/Lurkernomoreisay Jan 02 '26

what do you mean by LIFO order?

init vector

insert element at front (container.insert(container.begin(), 1))

insert element at back  (container.insert(container.end(), 2))

insert range of two elements at position 1 (container.insert_range(std::next(container.begin(), 1), std::list{3, 4, 5})

insert element at position 3, 3 copies (container.insert(std::next(container.begin(), 2), 3, 6)

// 1 3 6 6 6 4 5 2

sort vector // 1 2 3 4 5 6 6 6 ...

the vector has resized, and reallocated memory, and moved/copied all entries to the new memory range.   is destruction lifo of the user insertion order? lifo of the new memory area relocation reinsertion order? 

2

u/azswcowboy Jan 02 '26

I mean it’s a badly posed question - because my wetware brain immediately went to with what intervening container operations? If I call sort after some inserts is it still LIFO? That implies heavy state maintenance. You probably don’t mean that though. What you mean is erasing in the order end() to begin() - which if I wanted to ensure that I’d use the container erase algorithm on a reversed vector. Bc pretty sure for vector it’s not specified the element order.

https://en.cppreference.com/w/cpp/container/vector/erase2

Still, your thesis is correct - LLMs parrot all sorts of garbage constantly. And my recent experience with chatGPT 5 makes me think it regressed. Telling me about things in the standard, that are not in the standard.

1

u/The_JSQuareD Jan 02 '26

Well the 'correct' response by an LLM would be to note that complexity, and either ask for clarification from the user or state a reasonable assumption about what the user meant and proceed under that assumption. I think it's reasonable for OP to call out that none of the current LLMs seem to do that, and aside from not noting the hidden complexity, also give an answer which is simply wrong under the (implicit) assumptions that were made.

Of course, this isn't particularly surprising, but it is worth keeping in mind.

Beyond that, I learned something new myself, so I'm happy OP brought this up; I would have thought the standard makes some guarantee about the order of destruction, but apparently it doesn't. I think it should, though.