iterator equality

Discussion in 'C++' started by SW, Apr 5, 2010.

  1. SW

    SW Guest

    Hi, yesterday I came across a problem. I'd been sure that the snippet
    below is valid and evaluates to true:

    std::string::const_iterator() == std::string::const_iterator()

    (or for any other standard iterator e.g.
    std::vector<int>::const_iterator) and I was quite surprised that VC 2008
    fails on assertion at this (can be disabled by defining _SECURE_SCL 0
    and _HAS_ITERATOR_DEBUGGING 0). But, now I am no longer sure that this
    code is 100% valid. With asserts disabled it works as expected
    (evaluates to true) and so in g++ 4.2.3 on Linux.
    I can't imagine this wasn't valid because it simply doesn't make sense.
    For me it is as good as:

    typedef int * IntPtr;
    IntPtr() == IntPtr()

    So, should I change my code or just disable assertions and forget about it?

    Thanks,
    Szymon
    SW, Apr 5, 2010
    #1
    1. Advertising

  2. SW wrote, On 5.4.2010 20:21:
    > Hi, yesterday I came across a problem. I'd been sure that the snippet
    > below is valid and evaluates to true:
    >
    > std::string::const_iterator() == std::string::const_iterator()
    >
    > (or for any other standard iterator e.g.
    > std::vector<int>::const_iterator) and I was quite surprised that VC 2008
    > fails on assertion at this (can be disabled by defining _SECURE_SCL 0
    > and _HAS_ITERATOR_DEBUGGING 0). But, now I am no longer sure that this
    > code is 100% valid. With asserts disabled it works as expected
    > (evaluates to true) and so in g++ 4.2.3 on Linux.
    > I can't imagine this wasn't valid because it simply doesn't make sense.
    > For me it is as good as:
    >
    > typedef int * IntPtr;
    > IntPtr() == IntPtr()
    >
    > So, should I change my code or just disable assertions and forget about it?

    No. The assert is right. The following is excerpt from 24.1/5:

    "Iterators can also have singular values that are not associated with any
    container. [Example: After the declaration of an uninitialized pointer x (as
    with int* x;), x must always be assumed to have a singular value of a
    pointer. ] Results of most expressions are undefined for singular values; the
    only exception is an assignment of a non-singular value to an iterator that
    holds a singular value."

    Basically, the only thing you can do with singular iterator is to assign
    non-singular value to it. std::string::const_iterator() is singular iterator
    and thus comparison with it is undefined.

    --
    VH


    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.4.9 (MingW32)

    iEYEAREIAAYFAku6LekACgkQr2CghdezFMlDiQCeMAJkLC1LXXTjdth4RZ5k4Vbm
    C3oAn3T83CacRWeHNwMHivPD7yA4iEcr
    =7Qtm
    -----END PGP SIGNATURE-----
    Vaclav Haisman, Apr 5, 2010
    #2
    1. Advertising

  3. SW

    SW Guest

    Vaclav Haisman wrote:
    > SW wrote, On 5.4.2010 20:21:
    >> Hi, yesterday I came across a problem. I'd been sure that the snippet
    >> below is valid and evaluates to true:
    >>
    >> std::string::const_iterator() == std::string::const_iterator()
    >>
    >> (or for any other standard iterator e.g.
    >> std::vector<int>::const_iterator) and I was quite surprised that VC 2008
    >> fails on assertion at this (can be disabled by defining _SECURE_SCL 0
    >> and _HAS_ITERATOR_DEBUGGING 0). But, now I am no longer sure that this
    >> code is 100% valid. With asserts disabled it works as expected
    >> (evaluates to true) and so in g++ 4.2.3 on Linux.
    >> I can't imagine this wasn't valid because it simply doesn't make sense.
    >> For me it is as good as:
    >>
    >> typedef int * IntPtr;
    >> IntPtr() == IntPtr()
    >>
    >> So, should I change my code or just disable assertions and forget about it?

    > No. The assert is right. The following is excerpt from 24.1/5:
    >
    > "Iterators can also have singular values that are not associated with any
    > container. [Example: After the declaration of an uninitialized pointer x (as
    > with int* x;), x must always be assumed to have a singular value of a
    > pointer. ] Results of most expressions are undefined for singular values; the
    > only exception is an assignment of a non-singular value to an iterator that
    > holds a singular value."
    >
    > Basically, the only thing you can do with singular iterator is to assign
    > non-singular value to it. std::string::const_iterator() is singular iterator
    > and thus comparison with it is undefined.
    >
    > --
    > VH
    >


    Vaclav, Leight, thanks for your help.

    --
    Szymon
    SW, Apr 6, 2010
    #3
    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,404
  2. greg
    Replies:
    6
    Views:
    448
    Dietmar Kuehl
    Jul 17, 2003
  3. Replies:
    6
    Views:
    634
    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,137
    Steven D'Aprano
    Apr 20, 2007
  5. David Bilsby
    Replies:
    5
    Views:
    2,027
    David Bilsby
    Oct 9, 2007
Loading...

Share This Page