-
Notifications
You must be signed in to change notification settings - Fork 60
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Sparta::Buffer::erase(const iter&)
should return updated iter reference
#464
Comments
Looking more at the code, I'm also struggling to understand map/sparta/sparta/resources/Buffer.hpp Line 280 in 8b16b21
Why isn't the assertion firing based on |
Sparta::Buffer::erase(const iter&)
return type void
? Does Sparta::Buffer:erase(const iter&)
invalidate iterators? How to handle erase()
in an iterator loop?Sparta::Buffer::erase(const iter&)
should return updated iter reference
Also the warning comment at: map/sparta/sparta/resources/Buffer.hpp Lines 44 to 46 in 8b16b21
should probably have been removed at the same time the |
I started work on this, but since you've taken it on, I'll abandon the 5 minutes of effort I put into it. There are other changes to other resources I'd like to bundle with your changes, so I'll work on those separately (see #463 ). Glad you're looking into this. Admittedly, I've never really looked at this code since Steven built it many years ago. Ain't broke...don't fix. |
I only added some more detail and created the bug. Probably won't be able to look into a fix anytime soon. I've told people to work around it for now by not using the iterators and just use integer indices. So, don't let my comments keep you from looking at this. If I get a chance to actually work on it, I'll assign it to myself |
Ok, I started branch |
Yeah. If only based on what
|
I totally agree with your observations; it confused me too. Here are my thoughts on why
To make |
I follow the fact that Second bullet is a helpful reminder, yes, const casting is uni-directional. Third bullet would make sense to me if there was only a single implementation of The thing I was really getting confused by is why C++Reference was seeming to imply that there are two implementations of I also looked at the latest doxygen for GNU libstdc++ at https://gcc.gnu.org/onlinedocs/libstdc++/latest-doxygen/a08551.html and it does seem to indicate that there is only At this point, I'm only confused as to why you wouldn't have for (const_iterator itr = buffer.start(); itr != buffer.end();) {
if (something) {
itr = buffer.erase(itr);
} else {
itr++;
}
} and the assignment of What prevents someone from doing something really idiotic like: for (const_iterator itr = buffer.start(); itr != buffer.end();) {
if (something) {
iterator mutable_itr = buffer.erase(itr);
// aha! Now I can be nefarious and modify the element mutable_itr points to!
itr = mutable_itr;
} else {
itr++;
}
} Any thoughts on why you wouldn't have two implementations of |
I have no idea why the code was written this way. Maybe Steven had something in mind, then changed gears half way through? 🤷♂️ I think the better approach to this method -- if the iterator isn't valid, you can' increment it. Period. The way the code is written allows a user to increment a invalid iterator all day long, but NOTHING happens if the buffer has valid entries. It's ... weird. To your second points...
I think it might be pure laziness. To erase an element from a container, you just need the reference to the item you want to erase (for the container to find it). It can be const or not -- doesn't matter. The return also doesn't matter. Heck, if you can call I tried following the STL pattern for erase, but this will take a little more effort than I thought. I need to make a non-const iterator given a const one. Ech. |
Thanks for your replies. I'm glad it's not just me missing something. For the record, I think Dilip added to the goofiness to the increment operator when he added the reverse iterators. |
I managed to get |
Looking at the code on https://github.com/sparcians/map/tree/knutel/issue_464_resource_enhancements, I think it might be an improvement if Update: |
Discussed in #459
Originally posted by timsnyder December 18, 2023
In
map/sparta/sparta/resources/Buffer.hpp
Lines 663 to 683 in e876f58
The return types of
Sparta::Buffer::erase(const iter&)
arevoid
. Based on the STL semantics forerase()
, I would expect them to return a copy of the iterator adjusted for the erasure. DoesSparta::Buffer::erase()
invalidate the iterator?For example, with a
std::vector
, I would expect to need to handle iterator invalidation something like:Do I need to worry about the same type of thing with
Sparta::Buffer
or is the iterator somehow not invalidated bySparta::Buffer::erase()
?The text was updated successfully, but these errors were encountered: