I have a task for which you need to make your iterator, and then pass an iterator test for its performance. But I had a problem with checking the constant iterator. The iterator itself looks like this:
class const_iterator { public: typedef const_iterator self_type; typedef const std::pair<Key, mapped_type> value_type; typedef const value_type& reference; typedef const value_type* pointer; typedef std::forward_iterator_tag iterator_category; typedef int difference_type; //constructors const_iterator() { curr = nullptr; currPair = new value_type; printf("111\n"); } const_iterator(Tree_ &n) { printf("2"); curr = &n; currPair = new value_type; *currPair = std::make_pair(curr->key, curr->getValue()); } const_iterator(const self_type& other) { curr = other.curr; currPair = new value_type; *currPair = *other.currPair; } const_iterator(const std::pair<Key, mapped_type>& other) { curr = other.curr; currPair = other.currPair; } //operators //prefix increment self_type& operator++() { //сравни старое значение и новое if (currPair->second != curr->getValue()) curr->setValue(currPair->second); curr = successor(); // point to next node if (curr) *currPair = std::make_pair(curr->key, curr->getValue()); return *this; } // postfix increment (it++) self_type operator++(int) { self_type old = *this; ++(*this); return old; } //reference const reference operator*() const { return *currPair; } //pointer const pointer operator->() { return currPair; } // Inequality test operator bool operator!=(self_type const & other) const { return this->curr != other.curr; } // Dereference operator bool operator==(self_type const & other) const { return this->curr == other.curr; } operator iterator() const { return iterator(); } private: pointer currPair; Tree_* curr; //Successor Tree_* successor() { if (curr) if (curr->right != 0) { curr = curr->right; while (curr->left != 0) curr = curr->left; } else { Tree_* y = curr->parent; if (y == nullptr) return nullptr; while (curr == y->right) { curr = y; y = y->parent; if (y == nullptr) return nullptr; } if (curr->right != y) curr = y; } return curr; } }; check is stuck on the moment
using mp_it1 = mp::const_iterator; mp_it1 tmp1; static_assert(std::is_const<decltype(*tmp1)>::value, "returned value by dereferencing iterator is not const"); The compiler tells me:
Line Error C2338 returned value by dereferencing iterator is not const
Could you tell me what is wrong with my iterator, and how to fix it?