how to refer to the iterator_traits::reference of the iterator class?

112 Views Asked by At

i have tried to write the iterator class inside the custom container class i made and i defined the traits inside the class which made writing this line

reference operator*() const {return value->item;}

then after some research on the web i found a better way to do that which is define the iterator_traits inside the std namespace for the iterator class and then write my iterator class outside the container class which made the code more readable but the previous made error that i fixed by writing the template

T& operator*() const {return value->item;}

so i wonder if there is a way i still can use the first line of code

template <typename T>
class CustomIterator{
    using node = node<T>;
    node *value;
public:


    T& operator*() const {return value->item;}
    T& operator[] (size_t index) const { return *(index + *this); }

};

namespace std {
    template<typename T>
    class iterator_traits<CustomIterator<T> >
    {
    public:
        using difference_type = std::ptrdiff_t;
        using size_type = std::size_t;
        using value_type = T;
        using pointer = T*;
        using reference = T&;
        using iterator_category = std::random_access_iterator_tag;
    };
}
0

There are 0 best solutions below