Does std::list reference remains valid?

Started by
4 comments, last by dmatter 16 years ago
Is a reference to an object in a std::list safe? That is, if a method returns myList.back() can I assume that this reference will remain valid as long as the object stays in the list and there are no insertions in front of the object? I'll be using push_back() to put the objects in the list and they will won't be deleted until the end of the program. Thanks.
Advertisement
References, pointers and iterators to elements of an std::list will remain valid until the element is removed from the list (or the list is destroyed, etc.) no matter what you do to other elements in the list; add in front, add in back or even delete every other element in the list.
I'm fairly sure the reference will be valid as long as the object stays in the list (even if there is an insertion).

EDIT: Too slow.
[TheUnbeliever]
Wow! Even better than I was hoping.

Thanks for the quick responses also.

Unless, of course, this is an April Fool's thing...
No, not April fools. I just have first strike. If you want confirmation, you can look at section 23.2.2.3 paragraphs 1 and 3 in the C++ Standard, though all of 23.2.2 would be relevant.
I usually don't have a copy of the standard available ,so when I need a refresh on something STL I'll type, for example, sgi list into Google, click the list<T, Alloc> link and scroll right down to the Notes sections to read up on iterator stuff.

Of course it's important to bear in mind that the SGI implementation doesn't have to reflect the standard at all times, but I've yet to have a problem with it.

This topic is closed to new replies.

Advertisement