Iterator assignment from a pointer

Discussion in 'C++' started by Dom Gilligan, Aug 18, 2005.

  1. Dom Gilligan

    Dom Gilligan Guest

    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
    Dom Gilligan, Aug 18, 2005
    #1
    1. Advertising

  2. Dom Gilligan wrote:
    > 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
    Victor Bazarov, Aug 18, 2005
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Hendrik Maryns
    Replies:
    18
    Views:
    1,416
  2. greg
    Replies:
    6
    Views:
    453
    Dietmar Kuehl
    Jul 17, 2003
  3. Replies:
    6
    Views:
    640
    Jim Langston
    Oct 30, 2005
  4. Steven D'Aprano

    What makes an iterator an iterator?

    Steven D'Aprano, Apr 18, 2007, in forum: Python
    Replies:
    28
    Views:
    1,154
    Steven D'Aprano
    Apr 20, 2007
  5. David Bilsby
    Replies:
    5
    Views:
    2,037
    David Bilsby
    Oct 9, 2007
Loading...

Share This Page