-
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
Deleted iterator copy constructor/assignment and find
ing
#272
Comments
You might also ask why we don't just use for (const auto& p: mcparts) {
if (p.getPDG() == 11) {
// stuff
}
} The reason is that we need to do a few more of these searches, and have to combine the results. Nesting deeper and deeper just makes the code more cumbersome. |
Finally, the following quote from the link on c++20 range views above is interesting:
Those three are of course exactly the ones that are missing :-) |
Thanks for the nice summary and also pointers to the c++ standard requirements. Let me try and add a few podio details to the discussion.
If you want to have a go at this, we would be very happy to accept a PR for this. I am working on a bit of documentation for the template things, so that it should become possible for others, not as deeply into the whole thing as me to work on the templates. Just for completeness, we seem to have this on our list already (somehow): #150 but having another request for this might push this a bit further up :) |
I suspect there are good reasons to delete the iterators in python/template/macros/iterator/iterator.jinja2, but it makes searching through a collection a bit more cumbersome at times. I am wondering if there are ways in which some of the choices on iterator copy constructors and assignment operators deletion can be relaxed, or am I missing something I should be using instead?
The following are attempts at finding the first electron in an MCParticlesCollection. I am using gcc-11 with
-std=gnu++20
.Using
find_if
to get an iterator to the first electronThis fails because MCParticleCollectionIterator has a deleted copy constructor which is required (likely) to return a copy of the internal iterator inside std::find_if.
Using a simple (old-style) iterator loop
If we can't copy an iterator, then let's avoid the copy.
This fails because iterator post-increment is not implemented. Ok, easy enough to change (and arguably better though unlikely that an iterator takes up so much memory this matters here; still, it is good to instill best practices).
This still fails but now because we cannot assign p to mc_first_electron since the assignment operator for iterators is deleted. Ok, we can avoid this assignment too, by avoiding a locally scoped iterator.
Ah, we have something that compiles.
Checking that we actually found something
After the search, we want to make sure we found something (and return if not).
This now fails again because operator== isn't defined for iterators. We already used operator!= above, so we end up with this:
Ultimately, in a c++20 world,...
When I started off, I had hopes of being able to use the following compact syntax:
This does not compile (unsurprisingly) due to the missing operator| on collections. Implementing this doesn't seem too onerous but I think I'd need to understand first what the design decisions were for the deleted iterators and assignment operators.
The text was updated successfully, but these errors were encountered: