Boost logo

Boost :

From: Jeremy Siek (jsiek_at_[hidden])
Date: 2000-12-29 00:27:56

I tried changing to return by reference, but ran into really nasty
VC++ compiler bug. This was the cause of some of the headaches of
using iterator adaptors in the graph library. I suppose we could
return by reference as long as the compiler isn't VC++.

As for the concept checking, yes the concept checks would catch
the reference return type problem, at least on compilers that
have iterator_traits. Though perhaps there is a way around that...

  template <class TT>
  struct ForwardIteratorConcept
    void constraints() {
      function_requires< InputIteratorConcept<TT> >();
      function_requires< ConvertibleConcept<
        typename std::iterator_traits<TT>::iterator_category,
        std::forward_iterator_tag> >();
      typedef typename std::iterator_traits<TT>::reference reference;
      reference r = *i;
    TT i;

On Thu, 28 Dec 2000, David Abrahams wrote:

abraha> ----- Original Message -----
abraha> From: "Bernard HUGUENEY" <bernard.hugueney_at_[hidden]>
abraha> > Hi,
abraha> > I'm greatly interested by iterator_adaptor, and currently (ab)using it a
abraha> lot.
abraha> > It really made me rewrite as much of my own stuff as time permitted to
abraha> make my iterators "right".
abraha> > I'm currently experiencing troubles when composing some iterators:
abraha> > when I retrun a dereference() value, it is by reference, but if the
abraha> underlying iterator (as one from
abraha> > counting_iterator_policies) returns by value, the compiler complains that
abraha> I take the reference to the
abraha> > temporary underlying return value :-(
abraha> Hi Bernard,
abraha> Sorry it took me so long to respond.
abraha> If you are willing to have your composed iterator be only a ForwardIterator
abraha> or OutputIterator, you can return a non-reference type from dereference()
abraha> (All other iterator categories require that the type of a dereferenced
abraha> iterator be a reference type). The only other way to build an iterator whose
abraha> dereference() returns a reference atop an iterator which doesn't return a
abraha> reference type when dereferenced is to store the value somewhere (e.g. in
abraha> the iterator itself) so you can return a reference to it.
abraha> > Would it be possible to fix integer_range?
abraha> Hmm, I guess integer_range /is/ broken because its iterator_category is
abraha> random_access_iterator_tag but it returns a non-reference type from
abraha> dereference(). It looks to me like this would be trivial to fix.
abraha> > Or what would be the correct workaround ? It might be that
abraha> counting_iterator_policies is correct :-(
abraha> No, I think you've found a problem. Jeremy, is there a way to use concept
abraha> checks to make sure that people don't give iterator tags >= ForwardIterator
abraha> to things that return non-reference types from dereference?
abraha> > I guess that means I should specialize my iterator wrappers to hold a copy
abraha> > of the dereferenced iterator is it's iterator_traits<>::reference is not
abraha> one, but I'd rather not...
abraha> Sorry, I can't parse that sentence.
abraha> > A problem, I believe, lie in the fact that the refreturned is not const,
abraha> and dereference() takes a const iterator. I did not
abraha> > find any satisfying solution (my code uses const_cast<>), but I'no guru
abraha> myself :-)
abraha> >
abraha> > Anyway, thank you for the great work, I'm always more than happy to
abraha> rewrite code when it involves using some
abraha> > "treasures from the vault" :-)
abraha> <smile>

 Jeremy Siek www:
 Ph.D. Candidate email: jsiek_at_[hidden]
 Univ. of Notre Dame work phone: (219) 631-3906

Boost list run by bdawes at, gregod at, cpdaniel at, john at