You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
int main()
{
json a = {1, 2, 3};
json::reverse_iterator rit = a.rbegin();
++rit;
return 0;
}
gives:
error: ‘std::reverse_iterator<_Iterator>& std::reverse_iterator<_Iterator>::operator++() [with _Iterator = nlohmann::basic_json<>::iterator]’ is inaccessible
with g++
and
error: 'operator++' is a private member of 'std::reverse_iterator<nlohmann::basic_json<std::map, std::vector, std::basic_string, bool,
long, double, std::allocator>::iterator>'
with clang++
Thank You for Your quick reaction, this solves the problem mentioned above.
However, it seems, that ++ returns an std::reverse_iterator, and not a json::reverse_iterator.
int main()
{
json a = {1,2,3};
json::reverse_iterator rit = ++a.rbegin();
return 0;
}
trying to compile the above code gives the following error:
error: no viable conversion from 'std::reverse_iterator<nlohmann::basic_json<std::map, std::vector, std::basic_string, bool, long, double, std::allocator>::iterator>' to
'json::reverse_iterator
int main()
{
json a = {1,2,3};
json::reverse_iterator rit = a.rbegin();
++rit;
json b = {0,0,0};
std::transform(rit,a.rend(),b.rbegin(),[](json el){return el;});
std::cout<<b <<std::endl;
return 0;
}
compiles, and the output is : [0,1,2]
but trying to compile
int main()
{
json a = {1,2,3};
json b = {0,0,0};
std::transform(++a.rbegin(),a.rend(),b.rbegin(),[](json el){return el;});
std::cout<<b <<std::endl;
return 0;
}
I get this error:
error: no matching function for call to 'transform'
stl_algo.h:4915:5: note: candidate template ignored: deduced conflicting types for parameter '_InputIterator'
('std::reverse_iterator<...>' vs. 'nlohmann::basic_json<...>::reverse_iterator')
Interestingly, all code mentioned here compiles if you comment out the classes reverse_iterator and const_reverse_iterator and change the forward declarations in the beginning of json.hpp to
/// a reverse iterator for a basic_json containerusing reverse_iterator = std::reverse_iterator<iterator>;
/// a const reverse iterator for a basic_json containerusing const_reverse_iterator = std::reverse_iterator<const_iterator>;
However, the functions key() and value() which are available for iterator and const_iterator are not available to reverse_iterator or const_iterator. And I have no idea how to add them, because implementing the classes reverse_iterator and const_reverse_iterator yielded this issue... Any ideas?
When creating your reverse_iterator derived class, you should only need to implement those members that return std::reverse_iterator and make them return your own iterator type instead.
Ok, I could fix #100. The problem was that I needed to decrement base() before forwarding calls to it.
I made little progress in this issue, but still have problems with in-place usage of operator++ and operator++(int): There seems to be a problem converting std::reverse_iterator<json::iterator> to json::reverse_iterator, and I have no idea how to fix this...
In commit 19d550c, I implemented a reverse_iterator and const_reverse_iterator which allows to compile all code in this issue (also added it as unit test).
The code is not as pretty as I hoped (I am still waiting for answers on Statck Overflow), but it seems OK. I would be glad for any feedback (especially @gregmarr whose hints were very valuable!) on the commit. I especially dislike the fact that the const and non-const versions share a lot code, and the reverse versions are also quite generic.
I'm having trouble building unit.cpp under VS2015, and I'm not sure why, but this should give you a single implementation for both reverse_iterator and const_reverse_iterator. There were also some member functions in there that looked like they would be the same as the base class version, so I'm not sure if they're necessary. Feel free to change any names that you don't like.
template<typename Base> class json_reverse_iterator : public std::reverse_iterator<Base>
{
public:
/// shortcut to the reverse iterator adaptor
using base_iterator = std::reverse_iterator<T>;
/// create reverse iterator from iterator
json_reverse_iterator(const typename base_iterator::iterator_type& it)
: base_iterator(it) {}
/// create reverse iterator from base class
json_reverse_iterator(const base_iterator& it) : base_iterator(it) {}
/// post-increment (it++)
json_reverse_iterator operator++(int)
{
return base_iterator::operator++(1);
}
/// pre-increment (++it)
json_reverse_iterator& operator++()
{
base_iterator::operator++();
return *this;
}
/// post-decrement (it--)
json_reverse_iterator operator--(int)
{
return base_iterator::operator--(1);
}
/// pre-decrement (--it)
json_reverse_iterator& operator--()
{
base_iterator::operator--();
return *this;
}
/// add to iterator
json_reverse_iterator& operator+=(difference_type i)
{
base_iterator::operator+=(i);
return *this;
}
/// add to iterator
json_reverse_iterator operator+(difference_type i) const
{
auto result = *this;
result += i;
return result;
}
/// subtract from iterator
json_reverse_iterator operator-(difference_type i) const
{
auto result = *this;
result -= i;
return result;
}
};
using reverse_iterator = json_reverse_iterator<typename basic_json::iterator>;
using const_reverse_iterator = json_reverse_iterator<typename basic_json::const_iterator>;
Activity
fixed issue #93
nlohmann commentedon Jun 19, 2015
Thanks for the hint - this was a stupid mistake...
TmsKtel commentedon Jun 19, 2015
Thank You for Your quick reaction, this solves the problem mentioned above.
However, it seems, that ++ returns an std::reverse_iterator, and not a json::reverse_iterator.
trying to compile the above code gives the following error:
error: no viable conversion from 'std::reverse_iterator<nlohmann::basic_json<std::map, std::vector, std::basic_string, bool, long, double, std::allocator>::iterator>' to
'json::reverse_iterator
nlohmann commentedon Jun 20, 2015
This is strange.
works... I'll check.
TmsKtel commentedon Jun 22, 2015
compiles, and the output is : [0,1,2]
but trying to compile
I get this error:
error: no matching function for call to 'transform'
stl_algo.h:4915:5: note: candidate template ignored: deduced conflicting types for parameter '_InputIterator'
('std::reverse_iterator<...>' vs. 'nlohmann::basic_json<...>::reverse_iterator')
nlohmann commentedon Jun 24, 2015
Interestingly, all code mentioned here compiles if you comment out the classes
reverse_iteratorandconst_reverse_iteratorand change the forward declarations in the beginning ofjson.hpptoHowever, the functions
key()andvalue()which are available foriteratorandconst_iteratorare not available toreverse_iteratororconst_iterator. And I have no idea how to add them, because implementing the classesreverse_iteratorandconst_reverse_iteratoryielded this issue... Any ideas?nlohmann commentedon Jun 24, 2015
I asked StackOverflow for help: http://stackoverflow.com/questions/31036364/inherit-all-functions-from-user-defined-iterator-in-reverse-iterator
gregmarr commentedon Jun 24, 2015
When creating your reverse_iterator derived class, you should only need to implement those members that return std::reverse_iterator and make them return your own iterator type instead.
nlohmann commentedon Jun 27, 2015
Hi @gregmarr, unfortunately, I do not understand what you mean. I described my problem at http://stackoverflow.com/questions/31036364/inherit-all-functions-from-user-defined-iterator-in-reverse-iterator - maybe you can help?
gregmarr commentedon Jun 29, 2015
The only way I see to do it is something like this:
using mytype = reverse_iterator;
using mybase = std::reverse_iterator;
mytype &operator++()
{
mybase::operator++();
return *this;
}
mytype operator++(int)
{
return mytype(mybase::operator++(1));
}
and repeating that for these:
operator+=
operator+ (both member and non-member)
operator--
operator--(int)
operator-=
operator-
nlohmann commentedon Jul 6, 2015
Ok, I could fix #100. The problem was that I needed to decrement
base()before forwarding calls to it.I made little progress in this issue, but still have problems with in-place usage of
operator++andoperator++(int): There seems to be a problem convertingstd::reverse_iterator<json::iterator>tojson::reverse_iterator, and I have no idea how to fix this...@gregmarr , maybe you have an idea...
Cheers
Niels
gregmarr commentedon Jul 7, 2015
What code do you have that isn't working? I won't be able to look at it until Sunday due to vacation, but I can try then.
new implementation for (const_)reverse_iterator to cope with issue #93
nlohmann commentedon Jul 7, 2015
In commit 19d550c, I implemented a
reverse_iteratorandconst_reverse_iteratorwhich allows to compile all code in this issue (also added it as unit test).The code is not as pretty as I hoped (I am still waiting for answers on Statck Overflow), but it seems OK. I would be glad for any feedback (especially @gregmarr whose hints were very valuable!) on the commit. I especially dislike the fact that the const and non-const versions share a lot code, and the reverse versions are also quite generic.
gregmarr commentedon Jul 13, 2015
I'm having trouble building unit.cpp under VS2015, and I'm not sure why, but this should give you a single implementation for both reverse_iterator and const_reverse_iterator. There were also some member functions in there that looked like they would be the same as the base class version, so I'm not sure if they're necessary. Feel free to change any names that you don't like.
nlohmann commentedon Jul 13, 2015
Thanks for the code. I'll check!
closes #104 and integrates code from #93
Review round 2 (nlohmann#92)
Review round 2 (nlohmann#92)