Iterator assignment from a pointer

D

Dom Gilligan

What exactly can you assign to a set iterator, by assignment or
initialisation? (is this covered in Josuttis? I can't find it).

Some (limited) digging around in the Gnu code shows no operator=, but
2 ctors for iterators, and 3 for const_iterators. For const_iterator,
there's a no-parameter ctor, and a ctor which takes another
const_iterator. The 3rd one is more interesting - it takes a pointer
to a node in the R/B tree, and the iterator itself is actually a
pointer to a node.

This makes me think that there may actually be a way to assign a
pointer to an iterator, with the right runes. If there isn't one, can
anyone tell me why this isn't allowed?

Thanks

Dom
 
V

Victor Bazarov

Dom said:
What exactly can you assign to a set iterator, by assignment or
initialisation? (is this covered in Josuttis? I can't find it).

You mean like

set<int>::iterator it;

it = ??? // what is allowed here?

From what I see in the Standard, the iterators for containers are
implementation-defined, and the only requirement on the std::set iterators
is that they are of the bidirectional kind. The only assignment defined
for a bidirectional iterator is from another bidirectional iterator.
Some (limited) digging around in the Gnu code shows no operator=, but
2 ctors for iterators, and 3 for const_iterators. For const_iterator,
there's a no-parameter ctor, and a ctor which takes another
const_iterator. The 3rd one is more interesting - it takes a pointer
to a node in the R/B tree, and the iterator itself is actually a
pointer to a node.

The absence of an explicit operator= suggests that only the compiler-
generated copy assignment can be used. Just like I said, only assignment
from another iterator.
This makes me think that there may actually be a way to assign a
pointer to an iterator, with the right runes. If there isn't one, can
anyone tell me why this isn't allowed?

Assignment from a pointer will yield creation of a temporary iterator
from the pointer and the assignment from that iterator:

set<int>::iterator it;
it = somepointer;

is the same as

set<int>::iterator it;
it = set<int>::iterator(somepointer);

Since set<int>::iterator is implementation-defined, there is nothing
in C++ to say about what constructors it should or should not have.

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top