• Advertisement
Sign in to follow this  

Does std::list reference remains valid?

This topic is 3614 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Wow! Even better than I was hoping.

Thanks for the quick responses also.

Unless, of course, this is an April Fool's thing...

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement