Skip to content

reverse_iterator operator inheritance problem #93

Closed

Description

@TmsKtel
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++

Activity

  1. self-assigned this
    on Jun 19, 2015
  2. added a commit that references this issue on Jun 19, 2015
  3. nlohmann commented on Jun 19, 2015

    @nlohmann
    Owner

    Thanks for the hint - this was a stupid mistake...

  4. TmsKtel commented on Jun 19, 2015

    @TmsKtel
    Author

    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

  5. nlohmann commented on Jun 20, 2015

    @nlohmann
    Owner

    This is strange.

    int main()
    {
        json a = {1,2,3};
        json::reverse_iterator rit = a.rbegin();
        ++rit;
        return 0;
    }

    works... I'll check.

  6. TmsKtel commented on Jun 22, 2015

    @TmsKtel
    Author
    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')

  7. nlohmann commented on Jun 24, 2015

    @nlohmann
    Owner

    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 container
    using reverse_iterator = std::reverse_iterator<iterator>;
    /// a const reverse iterator for a basic_json container
    using 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?

  8. gregmarr commented on Jun 24, 2015

    @gregmarr
    Contributor

    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.

  9. nlohmann commented on Jun 27, 2015

    @nlohmann
    Owner

    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?

  10. gregmarr commented on Jun 29, 2015

    @gregmarr
    Contributor

    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-

  11. added this to the milestone on Jul 1, 2015
  12. nlohmann commented on Jul 6, 2015

    @nlohmann
    Owner

    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...

    @gregmarr , maybe you have an idea...

    Cheers
    Niels

  13. gregmarr commented on Jul 7, 2015

    @gregmarr
    Contributor

    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.

  14. nlohmann commented on Jul 7, 2015

    @nlohmann
    Owner

    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.

  15. gregmarr commented on Jul 13, 2015

    @gregmarr
    Contributor

    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>;
    
  16. nlohmann commented on Jul 13, 2015

    @nlohmann
    Owner

    Thanks for the code. I'll check!

  17. added a commit that references this issue on Jul 14, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions