Boost logo

Boost :

From: Daniel Wallin (dalwan01_at_[hidden])
Date: 2004-04-23 10:57:31


Vladimir Prus wrote:
> I'm again trying to use FP in C++, and having problems. What I wanted to do
> was:
>
> for_each(predecesor(v, g), /* BLL expression */ );
>
> where predecessor(..) returns a pair of 'transform_iterator' instances. The
> simplest example which illustrates the problem is:
>
> struct functor {
> typedef int result_type;
> int operator()(int i) const { return i+1; };
> };
>
> transform_iterator<functor, vector<int>::iterator> it(v.begin());
> (cout << _1)(*it);
>
> This does not compile, because operator* of transform_iterator return rvalue
> which can't be bound to non-const reference that operator() of the lambda
> expression accepts.
>
> Of all workarounds suggsted on BLL docs, only break_const is applicable to my
> case (I want to pass non-const references to the lambda, so const_parameters
> won't do), and break_const is scary.
>
> It looks like a serious problem with using FP, so I wonder what can be done.

How about what Peter Dimov suggested here:

   http://article.gmane.org/gmane.comp.lib.boost.devel/34663/match=+cref+

Something like:

   transform_iterator<functor, vector<int>::iterator> it(v.begin());
   boost::make_adaptable<void, int>(cout << _1)(*it);

-- 
Daniel Wallin

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