Boost logo

Boost :

From: Bernard HUGUENEY (bernard.hugueney_at_[hidden])
Date: 2000-10-25 09:27:31


Thank you very much Jeremy, useful is a misnomer, I've always tried to use as much iterators as possible to reuse algorithms, but as pointed out in iterator_adaptor this can be tedious ( and even boring :-( programming should be fun !)
As a result, I only implemented the subset of Iterator Concept requirement that was needed for my code to compile :-(
But no more !! I've seen the light and now everything can be an Iterator !

You do not want to see the complete error message ( really !!) but it ends with:

testDeepIter.cxx:107: instantiated from here
append_range.hpp:26: warning: returning reference to temporary

append_range helps me to iterate over 2 ranges,
I create an iterator consisting of:
        current position in frist range, end of first range
        current position in second range, begin of second range

the relevent line (append_range.hpp:26) is in the policies
of my iterator
 template< typename Reference, typename Iterator>
  Reference dereference( boost::type<Reference>, Iterator& x) const {
    if (x.first.first != x.first.second) return *(x.first.first);// line 26
    else return *(x.second.first) ;
  }

in the main program (testDeepIter.cxx:107)
 c_copy(make_append_range(boost::integer_range<long int>(0,5), make_constant_range(2,5)), outData);

You can see that the first range is an integer_range<long int>
so according to integer_range.hpp

struct counting_iterator_policies : public default_iterator_policies
{
    template <class IntegerType>
    IntegerType dereference(type<IntegerType>, const IntegerType& i) const
        { return i; }
};

it will return a long int by value. My append_range_iterator tries to return
a reference to the result of dereferencing the iterators in first or second range, et voila ! :-(

Note that I could avoid the problem by not using integer_range<>, but instead
my own state_machine_iterator ( thanks to you, EVERYTHING can now be an iterator, with suplied functor for increment and decrement. But my solution also has problem ( const correctness as I said in previous post).

I hope as was clear, and as you look interested in the use of iterator_adaptor, I include a copy of my test program.
It compiles happily ( once you remove the line using make_append_range(boost::integer_range<>) !) to produce the result:

deep iterator demo
1 2 3 4 0 0 5 6 7 8 0 0 9 10 11 12 0 0
unary_function_range: same but each element *2
2 4 6 8 0 0 10 12 14 16 0 0 18 20 22 24 0 0
counted_range, only first 5 elements
1 2 3 4 0
nullary_function_iterator and coupling : display 5 random numbers
48271 182605794 1291394886 1914720637 2078669041
constant range: display 5 times the same string
ToTo ToTo ToTo ToTo ToTo
state machine iterator : MN object in 5 states
2 4 8 16 32
append ranges
0 1 2 3 4 2 2 2 2 2 0 1 2 3 4 2 2 2 2 2
reverse ranges
4 3 2 1 0
symetric borders
2 1 1 2 3 4 4 3

If the boost mailing list does not remove my attached example testDeepIter.cxx, you'll know how much I owe you and Dave
for iterator_adaptor ! ( And I also use GGCL :-) for a neural network !).
So a big thank you for your contributions in boost!

Bernard

jsiek_at_[hidden] on 25/10/2000 15:23:00
Pour : boost_at_[hidden]@hub
cc : (ccc : Bernard HUGUENEY/IMA/DER/EDFGDF/FR)
Objet : Re: [boost] integer_range iterator return by value semantic

Hi Bernard,

Glad to hear iterator_adaptor has been useful to you.

Would you please post a short example demonstrating the problem
you describe?

Thanks!

Jeremy

----------------------------------------------------------------------
 Jeremy Siek www: http://www.lsc.nd.edu/~jsiek/
 Ph.D. Candidate email: jsiek_at_[hidden]
 Univ. of Notre Dame work phone: (219) 631-3906
----------------------------------------------------------------------

On 25 Oct 2000, Bernard HUGUENEY wrote:

> Hi,
> I'm greatly interested by iterator_adaptor, and currently (ab)using it a
 lot.
> It really made me rewrite as much of my own stuff as time permitted to make
 my iterators "right".
> I'm currently experiencing troubles when composing some iterators:
> when I retrun a dereference() value, it is by reference, but if the
 underlying iterator (as one from counting_iterator_policies) returns by
 value, the compiler complains that I take the reference to the temporary
 underlying return value :-(
>
> Would it be possible to fix integer_range ? Or what would be the correct
 workaround ? It might be that counting_iterator_policies is correct :-( I
 guess that means I should specialize my iterator wrappers to hold a copy
> of the dereferenced iterator is it's iterator_traits<>::reference is not
 one, but I'd rather not...
>
> A problem, I believe, lie in the fact that the refreturned is not const, and
 dereference() takes a const iterator. I did not
> find any satisfying solution (my code uses const_cast<>), but I'no guru
 myself :-)
>
> Anyway, thank you for the great work, I'm always more than happy to rewrite
 code when it involves using some
> "treasures from the vault" :-)
>
> Bernard
>
> ----
> European Union wants to know what people think of software patents !
> http://europa.eu.int/comm/internal_market/en/intprop/indprop/softpaten.htm
>
>
>
>




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