Boost logo

Boost Users :

From: JOAQUIN LOPEZ MU?Z (joaquin_at_[hidden])
Date: 2007-11-17 11:04:08


----- Mensaje original -----
De: Nebojsa Simic <nelle_at_[hidden]>
Fecha: Sábado, Noviembre 17, 2007 4:02 pm
Asunto: Re: [Boost-users] [multiindex] [bind] Update members of the
container
between two iterators
Para: boost-users_at_[hidden]

> JOAQUIN LOPEZ MU?Z wrote:
[...]
> > You can't use for_each(it1,it2,someFunctor) to do what you want
> because> modify() needs an iterator, and for_each passes a value
> reference> to someFunctor rather than the iterator.
> > So, you've got to resort to a handmade loop like:
>
> So, the current status is:
>
> // TEMPLATE FUNCTION for_each_it
> template<class _InIt,
> class _Fn1> inline
> _Fn1 for_each_it(_InIt _First, _InIt _Last, _Fn1 _Func)
> { // perform function for each element
> for (; _First != _Last; ++_First)
> _Func(_First);
> return (_Func);
> };
>
> and when I try to do this :
>
> for_each_it( it
> , idxResponse.end()
> , boost::bind( &StatisticsByResponse::modify, idxResponse, ::_1,
> fnAddStats )
> );
>
> still get compilation errors. What am I missing here ?
>
> as a temporary solution I added :
>
> inline bool bloodyBindable( StatisticsByResponse& index, const
> StatisticsByResponse::iterator& it,
> boost::function<void(Statistics&)> fnc )
> {
> return index.modify( it, fnc );
> }
>
> for_each_it( it
> , idxResponse.end()
> , boost::bind( bloodyBindable, idxResponse, ::_1, fnAddW0 )
> );
>
> which compiles, but I just want to know what am I doing wrong.

Two things:

1. The expression

  boost::bind(bloodyBindable,idxResponse,::_1,fnAddStats)

does a copy of the idxResponse index, which is not what you
want (indices are actually not copyable, more info on why you can
still get spurious index copies in certain compilers at
http://lists.boost.org/boost-users/2007/10/31777.php ). So,
to prevent this copying use boost::ref:

  boost::bind(bloodyBindable,boost::ref(idxResponse),::_1,fnAddStats)

More info on the usage of Boost.Ref with Boost.Bind at
http://boost.org/libs/bind/bind.html .

2. The reason why you can't do the binding directly and have to
resort to the bloodyBindable proxy is that modify is *not* a member
function, but a member function *template*. To do the binding you
have to provide an instantiation of modify, like this:

  for_each_it(
    it,idxResponse.end(),
    boost::bind(
      &StatisticsByResponse::modify<boost::function<void(Statistics)>
>,
      boost::ref(idxResponse),::_1,fnAddStats));

Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo


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