const_reverse_iterator
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: const_reverse_iterator

  1. #1
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,725

    const_reverse_iterator

    I've been creating a custom container and ran into this problem that has had me flumoxed all day.
    I'm sure I'm missing something obvious, but I just can't seem to see it.

    The code below fails with:-
    error C2440: 'return' : cannot convert from 'const int' to 'int &'

    The platform is Visual Studio 2013.

    The code compiles OK if 'it' is defined as an iterator, const_iterator or reverse_iterator, but not const_reverse_iterator.

    Code:
    #include <iterator>
    
    template <typename T>
    class Container
    {
    public:
    
      class iterator : public std::iterator<std::bidirectional_iterator_tag, T>
      {
      public:
    
        T& operator *()
        {
          return *p;
        }
    
        const T& operator *() const
        {
          return *p;
        }
    
        iterator& operator --()
        {
          return *this;
        }
    
        T* p;
      };
    
      class const_iterator : public std::iterator<std::bidirectional_iterator_tag, T>
      {
      public:
    
        const T& operator *() const
        {
          return *p;
        }
    
        const_iterator& operator --()
        {
          return *this;
        }
    
        T* p;
      };
    
      typedef std::reverse_iterator<iterator>       reverse_iterator;
      typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
    };
    
    int main()
    {
      Container<int>::const_reverse_iterator it;
    
      int i = *it;
    
      return 0;
    }
    "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
    Richard P. Feynman

  2. #2
    Join Date
    Oct 2008
    Posts
    1,168

    Re: const_reverse_iterator

    the reference_type of std::iterator<std::bidirectional_iterator_tag, T> is T&, hence the error ( you're passing a const int to an int& in reverse_iterator:perator* ).

    In order to solve the problem, you should explicitly set the appropriate reference and pointer type when deriving from std::iterator. Alternatively, you can use boost iterator facade/adaptor that will strip the constness of "const T" for you, or you can just derive from std::iterator<..., const T> at your own risk ( the resulting iterator will probably "work" most of the times, but it would not be strictly conforming ).

    BTW, note that your code is still lacking interoperability between the const and non-const iterators ( conversions, comparisons ... ).

  3. #3
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,725

    Re: const_reverse_iterator

    Quote Originally Posted by superbonzo View Post
    BTW, note that your code is still lacking interoperability between the const and non-const iterators ( conversions, comparisons ... ).
    Indeed, but I was trying to post the least amount of code that replicated the problem.
    "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
    Richard P. Feynman

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center