Boost logo

Boost :

Subject: Re: [boost] lifetime of ranges vs. iterators
From: Arno Schödl (aschoedl_at_[hidden])
Date: 2008-09-03 12:18:06


> > As with these end checks, what if dereference would throw exceptions? End checks gone, all the way up the stack...
> >

> Unfortunately not.

> This simply moves the check into the dereference operation
> Also, remember that a filter_iterator dereferences iterator
> in it's operator++, so you the number of comparisons is unchanged.

class filter_range {
   reference increment_and_dereference() throw( at_end ) {
      // If reference is value_type&, this method has
      // no local variables with dtor, so it needs no exception frame.
      // Building one would negate any performance gain.
      for(;;) {
         reference t=base.increment_and_dereference();
         if( pred(t) ) return t;
      }
   }

   void increment() {
      try {
         increment_and_dereference();
      } catch( at_end& ) {}
   }

   reference dereference() const {
      return base.dereference();
   }
};

class base_range() {
   ...

   reference increment_and_dereference() throw( at_end ) {
      if( empty() ) throw at_end();
      increment();
      return dereference();
   }
};

I have not thought about whether other adaptor_ranges (difference, union, ...) could also use the increment_and_dereference idiom, so that mixed-type stacks (which are the really interesting ones) could be built.

Arno

--
Dr. Arno Schoedl · aschoedl_at_[hidden] 
Technical Director 
 
think-cell Software GmbH · Invalidenstr. 34 · 10115 Berlin, Germany 
http://www.think-cell.com · phone +49-30-666473-10 · toll-free (US) +1-800-891-8091
Directors: Dr. Markus Hannebauer, Dr. Arno Schoedl · Amtsgericht Charlottenburg, HRB 85229

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk