Boost logo

Boost Users :

From: Pranckevicius, Tadas (tadas.pranckevicius_at_[hidden])
Date: 2006-06-09 05:45:19


>-----Original Message-----
>From: Benjamin Winkler [mailto:Benjamin.Winkler_at_[hidden]]
>Sent: 07 June 2006 22:20
>To: boost-users_at_[hidden]
>Subject: [Boost-users] Boost.Signals: strange bug in example program

>Hi,

>I just downloaded the newest boost library 1.33.1 and compiled the
>"maximum.cpp" example
>from the Signals library, which goes like this:

>#include <algorithm>
>#include <iostream>
>#include <boost/signals/signal2.hpp>

>template<typename T>
>struct maximum {
> typedef T result_type;

> template<typename InputIterator>
> T operator()(InputIterator first, InputIterator last) const
> {
> if (first == last)
> throw std::runtime_error("Cannot compute maximum of zero
elements!");
> return *std::max_element(first, last);
> }
>};

>int main()
>{
> boost::signal2<int, int, int, maximum<int> > sig_max;
> sig_max.connect(std::plus<int>());
> sig_max.connect(std::multiplies<int>());
> sig_max.connect(std::minus<int>());
> sig_max.connect(std::divides<int>());
>
> std::cout << sig_max(5, 3) << std::endl; // prints 15
>
> return 0;
>}

>As stated, the correct result would be 15 but the output really is 8.
>However, when I print the values via
> std::copy(first, last, std::ostream_iterator<T>(std::cout, " "));
>I get "8 15 2 1 ", which means that the values are calculated
correctly.

>This error occurs with Microsoft Visual Studio 2005 (8.0) as well as
MinGW >3.4.2.
>I even tried replacing std::max_element() with my own function, which
>expectedly
>didn't help.

>Seems like a bug to me, but I can't track it down. Could it be some
issue >with
>copying the iterator?

>Benjamin Winkler

Hello,

I tried to run this example and I got the same results, it prints 8 on
Microsoft Visual studio 2003 (7.1) as well.

I did try to debug it and ended up in "algorithm" file:

After adding cout:

template<class _FwdIt> inline
        _FwdIt _Max_element(_FwdIt _First, _FwdIt _Last)
        { // find largest element, using operator<
        _DEBUG_RANGE(_First, _Last);
        _FwdIt _Found = _First;
        cout<<*_Found<<endl;
        if (_First != _Last)
                for (; ++_First != _Last;) {
                        cout<<*_First <<endl;
                        if (_DEBUG_LT(*_Found, *_First))
                                _Found = _First;
                }
        return (_Found);
        }
It print's
8
15
2
1.66667

This is expected, but if I add to cout *_Found
Like this: cout<<*_First<<" "<<*_Found<<endl;
Results are:
8
8 8
8 8
8 8
So (_DEBUG_LT(*_Found, *_First) is always comparing the same numbers.

Something is wrong with overloaded operator *.

Smart people please help!!!

Thanks


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