Boost logo

Boost Users :

Subject: Re: [Boost-users] [signals] What's special about iterators passed to combiners?
From: Frank Mori Hess (frank.hess_at_[hidden])
Date: 2010-05-14 09:35:19


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Friday 14 May 2010, Boris Schaeling wrote:
> A reader of my book noticed that the sample program at
> http://en.highscore.de/cpp/boost/src/4.2.8/main.cpp which uses a combiner
> to return the smallest value of all slot values is buggy. The combiner's
> implementation is rather simple as iterators are simply forwarded to
> std::min_element():

min_element requires a forward iterator. The slot iterators are only input
iterators.

> Then I was trying to understand what's special about the iterators. Please
> look at the following code and guess whether the values printed will be
> different or not:
>
> T operator()(InputIterator first, InputIterator last) const
> {
> InputIterator temp = first;
> ++first;
> std::cout << *first << std::endl;
> std::cout << *temp << std::endl;
> }
>
> If you think you get two different values then you'll be as surprised as I
> am: Both iterators temp and first return the second value in the range
> [first, last).

temp no longer has to be valid after first is incremented (see definition of
input iterator).

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)

iEYEARECAAYFAkvtUZcACgkQ5vihyNWuA4VAlgCeI5+8kRxe0YOj0m9Pu1tBMgBJ
BvgAnApE7eG+SDyD0mZ9Ucca6oIvU9Zg
=0lZi
-----END PGP SIGNATURE-----


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net