This is the mail archive of the
libstdc++@gcc.gnu.org
mailing list for the libstdc++ project.
Re: [RFC] libstdc++/8780
- From: Joe Buck <jbuck at synopsys dot com>
- To: pcarlini at unitus dot it (Paolo Carlini)
- Cc: gdr at integrable-solutions dot net (Gabriel Dos Reis), libstdc++ at gcc dot gnu dot org (libstdc++ at gcc dot gnu dot org)
- Date: Tue, 3 Dec 2002 18:33:21 -0800 (PST)
- Subject: Re: [RFC] libstdc++/8780
> Gabriel Dos Reis wrote:
>
> >The submitter is confused. I would like to see the exact wording from
> >those paragraphs that may imply that a vector<>::iterator ought to be
> >a pointer.
> >
> >
> >| This seems to me non at all obvious! It is just a QoI issue?
> >| (in few other widespread implementations iterator *is* just a pointer)
> >
> >This is a QoI. I would expect a decent implementation to distinguish
> >a vector<>::iterator from a plain pointer.
> >
> >| Much more generally, I'm interested in knowing why the implementation
> >| of iterator was changed in such a way in the transition between v2 and v3.
> >
> Thanks Gaby for your feedback. I have changed the status to <feedback>,
> adding to the audit trial your comments.
This should be an FAQ, along with suggestions on how users can fix their
code that implicitly assumes that std::vector<T>::iterator is T*. Even
programmers who know better tend to make this mistake when they develop
code on compilers that use such an implementation.
The short answer is that, given
std::vector<T>::iterator iter_T;
then
&*iter_T
has type T* and points to the same object. As the elements of a vector
must be contiguous, this works portably.
It turns out that the question of whether the elements of a vector must
be contiguous was a defect report, #69. The resolution was "yes", so
the &* mechanism is guaranteed safe.
See
http://std.dkuug.dk/jtc1/sc22/wg21/docs/lwg-defects.html#69