Can someone explain why the following will not compile if I uncomment the line
foo::const_iterator j = f.begin();
, but if I use the line foo::const_iterator j = f.cbegin();
it will compile? I'm trying to get that line to work just as it works with my std::vector
example.
#include <vector>
struct foo {
struct node { };
node *first = nullptr, *last = nullptr;
struct base_iterator {
node* ptr;
base_iterator (node* n) : ptr(n) { }
};
struct iterator : base_iterator { using base_iterator::base_iterator; };
struct const_iterator : base_iterator { using base_iterator::base_iterator; };
iterator begin() { return iterator(first); }
const_iterator begin() const { return const_iterator(first); }
const_iterator cbegin() const { return const_iterator(first); }
};
// Test
int main() {
foo f;
foo::iterator i = f.begin();
// foo::const_iterator j = f.begin(); // Won't compile because f is not const.
// foo::const_iterator j = f.cbegin(); // Will compile fine.
std::vector<int> v;
std::vector<int>::const_iterator it = v.begin(); // Compiles even though v is not const.
}
It works for std::vector
because the iterators of all standard library containers are designed to support the iterator
--> const_iterator
conversion. It's meant to mimic how pointer conversions work.
Whenever your two iterators are user defined classes, you need to add it explicitly. You have two options:
A converting constructor:
struct iterator : base_iterator { using base_iterator::base_iterator; };
struct const_iterator : base_iterator {
using base_iterator::base_iterator;
const_iterator(const iterator& other) : base_iterator(other) {}
};
A conversion operator:
struct const_iterator : base_iterator { using base_iterator::base_iterator; };
struct iterator : base_iterator {
using base_iterator::base_iterator;
operator const_iterator() const { /* ... */ }
};