This is the mail archive of the libstdc++@gcc.gnu.org mailing list for the libstdc++ project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]