Skip to content
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

Make iterator operator++/--(int) equality-preserving #3332

Merged
merged 1 commit into from
Mar 8, 2022

Conversation

falbrechtskirchinger
Copy link
Contributor

@falbrechtskirchinger falbrechtskirchinger commented Feb 15, 2022

Commit f28fc22 introduced const qualifiers on post-(inc-/dec-)rement
operators of iterators. These qualifiers prevent the use of basic_json
in place of std::ranges::range, which requires the post-increment
operator to be equality-preserving.

These changes appear to be the result of ICC compiler suggestions, and
no further explanation is discernible from the PR discussion (#858).
Further testing revealed, that clang-tidy also suggests adding const to
prevent "accidental mutation of a temporary object".

As an alternative, this commit partially reverts f28fc22, removing all
added const qualifiers from return types and adds lvalue reference
qualifiers to the operator member functions instead.

Unit tests ensure the operators remain equality-preserving and
accidental mutation of temporaries following post-(inc-/dec-)rement is
prohibited.

Fixes #3331.

@coveralls
Copy link

coveralls commented Feb 15, 2022

Coverage Status

Coverage remained the same at 100.0% when pulling b926357 on falbrechtskirchinger:fix-3331 into d8a6329 on nlohmann:develop.

@falbrechtskirchinger falbrechtskirchinger changed the title Remove const from iterator operator return types Make iterator operator++/--(int) equality-preserving Feb 15, 2022
Commit f28fc22 introduced const qualifiers on post-(inc-/dec-)rement
operators of iterators. These qualifiers prevent the use of basic_json
in place of std::ranges::range, which requires the post-increment
operator to be equality-preserving.

These changes appear to be the result of ICC compiler suggestions, and
no further explanation is discernible from the PR discussion (nlohmann#858).
Further testing revealed, that clang-tidy also suggests adding const to
prevent "accidental mutation of a temporary object".

As an alternative, this commit partially reverts f28fc22, removing all
added const qualifiers from return types and adds lvalue reference
qualifiers to the operator member functions instead.

Unit tests ensure the operators remain equality-preserving and
accidental mutation of temporaries following post-(inc-/dec-)rement is
prohibited.

Fixes nlohmann#3331.
@falbrechtskirchinger
Copy link
Contributor Author

I've added NOLINT(cert-dcl21-cpp) and removed the use of variable templates from the unit tests (std::is_same_v).
Now waiting for CI runs to be approved by maintainer.

Copy link
Owner

@nlohmann nlohmann left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me.

@nlohmann nlohmann self-assigned this Mar 8, 2022
@nlohmann nlohmann added this to the Release 3.10.6 milestone Mar 8, 2022
@nlohmann nlohmann merged commit 700b95f into nlohmann:develop Mar 8, 2022
@nlohmann
Copy link
Owner

nlohmann commented Mar 8, 2022

Thanks a lot!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Iterator doesn't satisfy std::incrementable because post-increment may change constness
3 participants