Re: Copying singular iterators
On Mar 7, 5:11 am, a...@acm.org ("Andrew Koenig") wrote:
"Sylvain Pion" <Sylvain.P...@sophia.inria.fr> wrote in message
news:esje06$r76$1@news-sop.inria.fr...
I am wondering why there is such a strict requirement on singular values
of iterators. Iterators are meant to be small pointer-like objects,
and I can't see any useful reason to disallow copies/assignments of
singular values (which would of course propagate the "singular-ness").
One reason is that copying an uninitialized pointer is undefined:
int* p;
int* q = p; // undefined behavior
So the requirement on singular iterator values is necessary in order to
allow pointers to be iterators.
However, I think the following code is legal:
typedef int* int_ptr;
int_ptr p = int_ptr(); // default ctor on 'built-in-type' assignes
NULL
assert(p == NULL); // p has the 'singular' value for int_ptr
int_ptr q = p; // the 'singular' value is copied. (q is copy
constructed)
q = p; // now the value of p is also assigned to q (redundant, for
demonstration purposes only)
for (; p != q; ++p) // NULL can be compared, but should not be
incremented
do_something(*p); // this line should only be called with non-
singluar int_ptr values
so, why shouldn't the following be legal?
list<int>::iterator p = list<int>::iterator(); // use default ctor
to assign the singular value
list<int>::iterator q = p; // the 'singular' value is copied. (q is
copy constructed)
q = p; // now the value of p is also assigned to q (redundant, for
demonstration purposes only)
for (; p != q; ++p) // compare two singular values
do_something(*p); // this line should only be called with non-
singluar iterator values
in my code I have been indicating empty ranges of T with default
constructed vector<T>::iterators and I was disappointed when moving to
VC8 (iterator debug mode) to find that the standard indeed prohibits
this. I still wonder why.
Maarten Hilferink
Object Vision
Netherlands
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]