|
Boost : |
From: Jaakko Jarvi (jajarvi_at_[hidden])
Date: 2004-04-23 10:25:32
On Apr 23, 2004, at 10:04 AM, 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.
> 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.
>
Forwarding problem again :(
For a limited number of placeholders (up until _3) or so,
we could provide the extra overloads to make this problem go away.
N-ary operator() requires 2^N overloads, so for current BLL it is
feasible, for bind with placeholders up to _9 (or more?) not really.
Jaakko
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk