tsnorri
tsnorri

Reputation: 2097

Const correctness with Boost iterator_facade

I'm writing an iterator facade for use with a collection which is wrapped into another class. I followed the tutorial until telling the truth, which states:

Now node_iterator and node_const_iterator behave exactly as you'd expect... almost. We can compare them and we can convert in one direction: from node_iterator to node_const_iterator. If we try to convert from node_const_iterator to node_iterator, we'll get an error when the converting constructor tries to initialize node_iterator's m_node, a node* with a node const*. So what's the problem?

To me this seems exactly what I would want, i.e. not being able to convert a const iterator to a non-const iterator. The conversion being possible, seemingly, would be a failure in const correctness as the iterated node would lose the const qualifier.

Am I missing something? I'm asking because some of my code fails to compile and this is one possible reason.

Upvotes: 0

Views: 220

Answers (1)

Lightness Races in Orbit
Lightness Races in Orbit

Reputation: 385194

You can't just stop reading half-way through the chapter. The very next lines of the source material tell you why this is a problem: we want the conversion itself to not exist. As it is, the conversion exists, but fails someway up the call stack in the innards of the implementation, which results in less than clear diagnostics, and a broken boost::is_convertible<node_const_iterator,node_iterator> trait.

Upvotes: 2

Related Questions