Topic: Validity of an iterator after destrruction of container which created it.


Author: James Kanze <james.kanze@gmail.com>
Date: Thu, 14 Jan 2010 12:48:14 CST
Raw View
Just wondering, because I'm not even sure where to find this in
the standard, but what can you legally do with an iterator after
the container which created it has been destructed.  Obviously,
you can't dereference it, nor increment it.  But can you still
compare it to another iterator issued from the same container?
(I'd say no, but I can't find anything clearly one way or the
other in the standard.)  And more importantly, can you call its
destructor, e.g. would something like the following be legal:

       void f(std::deque< std::string >& v)
       {
               assert(! v.empty());
               std::string::const_iterator iter = v.front().begin();
               //  ...
               v.pop_front();
       }

I'd hope so, but at least one compiler I am using crashes
in similar cases, in the code for the destructor of the
iterator.  And I can't find anything in the standard which says
one way or the other.

--
James Kanze

--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use
mailto:std-c++@netlab.cs.rpi.edu<std-c%2B%2B@netlab.cs.rpi.edu>
]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: =?ISO-8859-1?Q?Martin_Vejn=E1r?= <avakar@technika.junior.cz>
Date: Thu, 14 Jan 2010 22:31:06 CST
Raw View
On Jan 14, 7:48 pm, James Kanze <james.ka...@gmail.com> wrote:
> Just wondering, because I'm not even sure where to find this in
> the standard, but what can you legally do with an iterator after
> the container which created it has been destructed.

C++03 only allows assignment of non-singular value into a singular
operator, but the intent probably was to allow destruction as well.
There is a defect report regarding this issue:
http://std.dkuug.dk/jtc1/sc22/wg21/docs/lwg-active.html#407. The
resolution has already been incorporated in the latest working draft.

Best regards,
--
Martin


--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: "Johannes Schaub (litb)" <schaub-johannes@web.de>
Date: Thu, 14 Jan 2010 22:31:02 CST
Raw View
James Kanze wrote:

> Just wondering, because I'm not even sure where to find this in
> the standard, but what can you legally do with an iterator after
> the container which created it has been destructed.  Obviously,
> you can't dereference it, nor increment it.  But can you still
> compare it to another iterator issued from the same container?
> (I'd say no, but I can't find anything clearly one way or the
> other in the standard.)  And more importantly, can you call its
> destructor, e.g. would something like the following be legal:
>
>        void f(std::deque< std::string >& v)
>        {
>                assert(! v.empty());
>                std::string::const_iterator iter = v.front().begin();
>                //  ...
>                v.pop_front();
>        }
>
I'm in the impression that using iterators in any other way than assigning
to them or destroying it is UB. 24.2/5 in n3000 says:

"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.        end example ] Results of most expressions are undefined for
singular values; the only exceptions are destroying an iterator that holds a
singular value and the assignment of a non-singular value to an iterator
that holds a singular value. In this case the singular value is overwritten
the same way as any other value. Dereferenceable values are always non-
singular."

I read that as saying the above code is fine. The iterator when not being
associated with a string anymore becomes singular.

--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]