-
Notifications
You must be signed in to change notification settings - Fork 32
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
daw_json_link's internal iterators behave differently than standard iterators #432
Comments
The documentation could be better here, but it has never been an issue either as it's intended to fill containers which will only call distance on forward iterators which they sometimes are. Right now, it allows much cheaper filling. I could add an option to fulfill input fully however, but in the mean time a distance like the following would work std::ptrdiff_t distance( auto first, auto last ) {
std::ptrdiff_t count = 0;
while( first != last ) {
*first;
++count;
++first; // so it doesn't look weird
}
return count;
} It will generally not be best to reserve the vector based on the size here though, unless the iterator passed to your constructor is forward. It sometimes is when the size is known because that member was in a different order to that needed. The cost of parsing will probably be less than that of allocation. What is often fruitful is to guess, in the static size case that probably isn't needed though as there is no allocation. |
Ah, it's been quite a while since I looked at iterators in C++, for some reason I completely forgot about input iterators (iterators which only support a single pass). Sorry about the confusion. So the only difference between daw_json_link's iterator and conventional input iterators is that you must dereference the iterator before incrementing. Probably not a big deal, but also a good idea to mention in the documentation. Does iterators in daw_json_link support |
The iterator does not fullfill input either because of op++ and equality comparable. They do work in all 3 of the standard implementations however for constructing/inserting into containers. Adding this as an option isn’t much, it just hasn’t been needed. |
Sounds fine to me, should I close this unless you want to keep track of documentation update? |
so I'm still trying to get my custom map data structure working as described in Issue 428, I have the modified simple example as below:
The only difference from the example you gave in Issue 428 is that I try to pre-allocate the underlying vector with the correct size, by calling
my_distance(first, last)
, which is a modified dumb version ofstd::distance()
.With this code,
my_distance()
raises a runtime error, as it goes in an infinite loop because it seems like++first
does nothing unless you evaluate the iterator by calling*first
first. This is different than how iterators work by convention. In fact, this is whystd::distance()
will not work on these iterators.If I uncomment the
*first
line, then this code will raise adaw::json::v3_24_0d::json_exception
, with the errorUnexpected end of data
. This implies that even though the iterators are pass-by-value to themy_distance()
function, incrementing the iterator copies impacted the original iterator. Again, this is different than how iterators work by convention.In summary:
Since this is very non-standard, if things have to stay this way, it should be clearly documented. Also, is there anyway I can find out the size of the iterator range, even if it takes linear time to do so?
The text was updated successfully, but these errors were encountered: