Json: [Question] Access to end() iterator reference

Created on 17 Sep 2018  路  5Comments  路  Source: nlohmann/json

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.

https://github.com/nlohmann/json/blob/186c747a19c56c3208e7539713607211b6059ceb/single_include/nlohmann/json.hpp#L5304

question proposed fix

Most helpful comment

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

All 5 comments

Could you provide an example?

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

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

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.

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

Was this page helpful?
0 / 5 - 0 ratings