Boost logo

Boost :

From: David Abrahams (dave_at_[hidden])
Date: 2004-04-23 10:37:43


Vladimir Prus <ghost_at_[hidden]> writes:

> 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.
> Maybe, transform_iterator should have yet another template parameter telling
> if it should store a value internally, and return reference (i.e. lvalue) in
> that case? It need not be enabled by default, but functions like my
> 'predecessor' above would make use of that extra template parameter.

I brought up this issue a few weeks ago. I was thinking we should be
able to write
     
     rvalue(_1)

or maybe

     cref(_1)

to pass rvalues.

-- 
Dave Abrahams
Boost Consulting
http://www.boost-consulting.com

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