0

I'm writting a STL like vector and an iterator class for it. And today while writting erase func which takes const_iterator, it turned out that there is not the conversation from iterator to const_iterator.I tried to find some information, but I still don't know how to make it. But STL vector makes it somehow. My Iterator class:

template<typename VectorDataType>
class Iterator{
    public:
            using value_type = VectorDataType;
            using difference_type = int;
            using pointer = VectorDataType*;
            using reference = VectorDataType&;
            using iterator_category =  std::random_access_iterator_tag;
   public:
            explicit Iterator(VectorDataType* ptr = nullptr)
               : ptr(ptr){}
             Iterator(const Iterator<VectorDataType>& iter)
                : ptr(iter.ptr){}
            ~Iterator() {ptr = nullptr;}

             template<template<typename Y> class Alloc>
             operator typename Vector<VectorDataType, Alloc>::const_iterator (){
                   typename Vector<VectorDataType, Alloc>::const_iterator citer(*this);
                 return citer;
             }

            Iterator& operator=(VectorDataType* rhs) {this->ptr = rhs; return *this;}
            Iterator& operator=(const Iterator& rhs) {this->ptr = rhs.ptr; return *this;}


            operator bool() const{
                bool result = (this->ptr) ? true : false;
                return result;
            }

            bool operator==(const Iterator<VectorDataType>& iter) const{
                    return this->ptr == iter.ptr;
            }
            bool operator!=(const Iterator<VectorDataType>&iter) const{
                return this->ptr != iter.ptr;
            }

            bool operator<(const Iterator<VectorDataType>& iter)const {
                return this->ptr < iter.ptr;
            }
            bool operator>(const Iterator<VectorDataType>& iter)const {
                return this->ptr > iter.ptr;
            }

            bool operator<=(const Iterator<VectorDataType>& iter)const {
                return this->ptr <= iter.ptr;
            }
            bool operator>=(const Iterator<VectorDataType>& iter)const {
                return this->ptr >= iter.ptr;
            }

            Iterator<VectorDataType>& operator+=(const difference_type& offset){
                this->ptr +=  offset;
                return *this;
            }
            Iterator<VectorDataType>& operator-=(const difference_type& offset){
                this->ptr -= offset;
                return *this;
            }

            Iterator<VectorDataType>& operator++(){
                this->ptr++;
                return *this;
            }
            Iterator<VectorDataType>& operator--(){
                this->ptr--;
                return *this;
            }

            Iterator<VectorDataType> operator++(int){
                Iterator<VectorDataType> temp(*this);
                this->ptr++;
                return temp;
            }
            Iterator<VectorDataType> operator--(int){
                Iterator<VectorDataType> temp(*this);
                this->ptr--;
                return temp;
            }

            Iterator<VectorDataType> operator+(const difference_type&offset) const{
                VectorDataType* newPtr = ptr;
                newPtr += offset;
                return Iterator{newPtr};
            }
            Iterator<VectorDataType> operator-(const difference_type&offset) const{
                VectorDataType* newPtr = ptr;
                newPtr -= offset;
                return Iterator{newPtr};
            }

            difference_type operator-(const Iterator<VectorDataType>& iter)const{
                return std::distance(iter.ptr, this->ptr);
            }

            VectorDataType& operator*(){return  *ptr;}
            const VectorDataType& operator*()const{return  *ptr;}
            VectorDataType* operator->(){return ptr;}


            VectorDataType& operator[](int offset){
                return ptr[offset];
            }


   private:
        VectorDataType* ptr;
};

Main's code:

auto list = {1, 2, 3, 4, 5, 6, 7, 8, 9};
    Vector<int> vec(list);

    for(auto&i: vec)
       std::cout << i << "\t";

    auto iter = vec.begin();
    iter++;
    iter++;
    iter++;

    auto pos = vec.erase(iter);

    for(auto&i: vec)
       std::cout << i << "\t";

erase definition:

iterator erase(const_iterator pos);

Console log:

g++ -c -pipe -g -std=gnu++1z -Wall -Wextra -fPIC  -I../MyVector -I. -I../../Qt/5.14.1/gcc_64/mkspecs/linux-g++ -o main.o ../MyVector/main.cpp
../MyVector/main.cpp: In function ‘int main()’:
../MyVector/main.cpp:49:30: error: no matching function for call to ‘Vector<int>::erase(Iterator<int>&)’
     auto pos = vec.erase(iter);
                              ^
In file included from ../MyVector/main.cpp:2:0:
../MyVector/MyVector.h:601:21: note: candidate: Vector<T, Allocator>::iterator Vector<T, Allocator>::erase(Vector<T, Allocator>::const_iterator) [with T = int; Allocator = std::allocator; Vector<T, Allocator>::iterator = Iterator<int>; Vector<T, Allocator>::const_iterator = Iterator<const int>]
            iterator erase(const_iterator pos){
                     ^~~~~
../MyVector/MyVector.h:601:21: note:   no known conversion for argument 1 from ‘Iterator<int>’ to ‘Vector<int>::const_iterator {aka Iterator<const int>}’
../MyVector/MyVector.h:618:21: note: candidate: Vector<T, Allocator>::iterator Vector<T, Allocator>::erase(Vector<T, Allocator>::const_iterator, Vector<T, Allocator>::const_iterator) [with T = int; Allocator = std::allocator; Vector<T, Allocator>::iterator = Iterator<int>; Vector<T, Allocator>::const_iterator = Iterator<const int>]

If you want to see all code, here is a github link: https://github.com/RRRadicalEdward/Vector/blob/master/MyVector.h

  • 1
    Have you considered taking a look at how `const_iterator` and `iterator` are implemented for the `std::vector` class? It works there, so you should take your ideas from working examples. – PaulMcKenzie Jun 18 '20 at 13:30

2 Answers2

1

The same way as all user defined conversions:

Either

  • implicit converting constructor in the target type or
  • implicit conversion operator in the source type
eerorika
  • 232,697
  • 12
  • 197
  • 326
1

The problem with your implicit conversion operator is that it puts the template-template parameter Alloc in a non-deduced context, that is, left to the scope operator:

template <template <typename Y> class Alloc>
operator typename Vector<VectorDataType, Alloc>::const_iterator() {
//                           non-deduced ~~~~^
    typename Vector<VectorDataType, Alloc>::const_iterator citer(*this);
    return citer;
}

Thus the compiler cannot apply this conversion. Try changing it to:

operator Iterator<const VectorDataType>() const {
    Iterator<const VectorDataType> citer(ptr);
    return citer;
}

Note also that the constructor call argument is a pointer, not the iterator (*this) itself.

Piotr Skotnicki
  • 46,953
  • 7
  • 118
  • 160