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

Can't use basic_json::iterator as a base iterator for std::move_iterator #233

Closed
robertmrk opened this issue Apr 13, 2016 · 2 comments
Closed

Comments

@robertmrk
Copy link
Contributor

While it's possible to move a range of container elements instead of copying them by using the std::move algorithm instead of std::copy, some copying algorithms like std::copy_if and std::copy_n doesn't have a move counterpart. The only way to use move semantics with these algorithms is to wrap the input iterators into std::move_iterator objects.

json source = {"a", "b", "c"};  
json dest;
std::copy_n(std::make_move_iterator(source.begin()), 2, std::back_inserter(dest));

Unfortunately, creating a move_iterator from a basic_json::iterator is not possible, because the indirection operator of move_iterator is a const member function and it can't call the non-const indirection operator of basic_json::iterator.
Compiling the code above with AppleClang 7.0.0 (which is based on clang 3.7 I think), will produce the following error:

../include/c++/v1/iterator:959:37: error: indirection requires pointer operand ('const nlohmann::basic_json<std::map, std::vector, std::__1::basic_string<char>, bool, long long, unsigned long long, double, std::allocator>::iterator' invalid)
      return static_cast<reference>(*__i);
                                    ^~~~

The issue can be fixed by making the indirection operator of basic_json::iterator const, and while we're at it, we can safely make the dereference operator const too, since the non-const iterators in STL have a const indirection and dereference operator too.

@nlohmann
Copy link
Owner

Thanks a lot!!!

@robertmrk
Copy link
Contributor Author

Don't mention it. I'm glad I could help.

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

No branches or pull requests

2 participants