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

[Question] Access to end() iterator reference #1242

Closed
loumalouomega opened this issue Sep 17, 2018 · 5 comments
Closed

[Question] Access to end() iterator reference #1242

loumalouomega opened this issue Sep 17, 2018 · 5 comments
Labels
kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation

Comments

@loumalouomega
Copy link

loumalouomega commented Sep 17, 2018

How can I access to the reference of the .end() iterator when there is an assert that forces to avoid it?

Maybe the question could look a little bit silly but I have problems with iterators because of this, and of course commenting the assert is just a short term and dirty solution to the problem.

assert(m_it.object_iterator != m_object->m_value.object->end());

@nlohmann
Copy link
Owner

Could you provide an example?

@loumalouomega
Copy link
Author

We are doing a migration from rapidjson to your library:

https://github.com/KratosMultiphysics/Kratos/blob/07929fd8063e68dd39747cb9b8c13f91e949e128/kratos/includes/kratos_parameters.h

We have a custom class that uses internally your library, so for that reason we create an iterator adaptor. During the construction of our begin() and end() methods we call to the operator*(), both to the end() and begin(), but the end() iterators give us the error due to the assert.

https://github.com/KratosMultiphysics/Kratos/blob/07929fd8063e68dd39747cb9b8c13f91e949e128/kratos/includes/kratos_parameters.h#L241

https://github.com/KratosMultiphysics/Kratos/blob/07929fd8063e68dd39747cb9b8c13f91e949e128/kratos/includes/kratos_parameters.h#L895

@gregmarr
Copy link
Contributor

It is illegal to dereference an end iterator, from ANY container.

@nlohmann
Copy link
Owner

You can turn off assertions by defining the preprocessor macro NDEBUG, see https://en.cppreference.com/w/cpp/error/assert. But as @gregmarr described, you must never dereference a past-the-end iterator in the first place.

@loumalouomega
Copy link
Author

Thanks, I will do that, I close this, thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation
Projects
None yet
Development

No branches or pull requests

3 participants