Boost logo

Boost :

From: Vladimir Prus (ghost_at_[hidden])
Date: 2004-04-23 10:04:59


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.

- Volodya


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