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] Interpretation of DR 198 resolution


On Tue, Nov 11, 2003 at 11:47:41AM +0100, Paolo Carlini wrote:
> I'm reading this old DR and its resolution [WP]:
>   
>    http://anubis.dkuug.dk/jtc1/sc22/wg21/docs/lwg-defects.html#198
> 
> The rationale points out that "... it is /not/ an issue about the
> behavior of predefined iterators" but still I'm quite a lot worried
> by the change to paragraph 1 of 24.4.1.3.3 [lib.reverse.iter.op.star]
> from the current:
>  
>    Iterator tmp = current;
>    return *--tmp;
> 
> that we are /closely/ following in our implementation, to
> 
>    this->tmp = current;
>    --this->tmp;
>    return *this->tmp;
> 
> What does this really mean? Perhaps that we are entitled to have the
> current implementation only provided we offer guarantees about the
> validity of pointers and references after iterator destruction?

Suppose I define an iterator that behaves like the iota operator in APL:

  class Ioterator : 
    public std::iterator<std::bidirectional_iterator_tag, int>
  {
    int i;
  public:
    Ioterator() : i(0) {}
    int& operator*() { return i; }
    Ioterator& operator++() { ++i; return *this; }
    Ioterator operator++(int) { Ioterator t(*this); ++*this; return t; }
    Ioterator& operator--() { --i; return *this; }
    Ioterator operator--(int) { Ioterator t(*this); --*this; return t; }
  };

and make a reverse iterator of it

  int i = *std::reverse_iterator<Ioterator>(Ioterator());

The problem is that operator* returns a reference to an int, but
the int is a member of an object that does not exist any more
when operator* returns.  This isn't a problem with ordinary 
iterators; they return a reference to a member of some other 
data structure that necessarily has a longer lifetime than
any iterator that points into it.

So, there's nothing useful we can guarantee.  We're not about to 
say that pointers into the stack are OK after the thing they point
to goes out of scope.

Hoping this helps...

Nathan Myers
ncm-nospam@cantrip.org


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