iterator equality

S

SW

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
 
V

Vaclav Haisman

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-----
 
S

SW

Vaclav said:
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.

Vaclav, Leight, thanks for your help.
 

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

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,169
Latest member
ArturoOlne
Top