-
-
Notifications
You must be signed in to change notification settings - Fork 7.3k
Description
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.