Boost logo

Boost :

From: Martin Schulz (Martin.Schulz_at_[hidden])
Date: 2007-08-13 08:13:45


Giovanni, sorry for the late reply.

> Consider this:
>
> std::vector<int> x = ....
> for_each(filter(x, some_filter_op), do_something);
>
> 'Filter' will obviously return a proxy for 'x' (actually
> wrapping x iterators with boost::filter iterators) as a
> temporary object.
> for_each will bind the temporary to a const reference (at
> least until we get rvalue references).

The fact that temporary objects cannot be passed in as non-const
references had been decided to avoid programming accidents in that
modifications to the temporary object were assumed to be
meaningfull and should be preserved. If you want this object as
non-const nevertheless, simply give a name to it.

> This means that with option 1, do_something will get const
> references to int, thus cannot mutate the integers in x.

But with a slight modification I do not see any more problem:

std::vector<int> x = ....
filter_type f = filter(x, some_filter_op);
for_each(f, do_something);

Here, f is passed as non-const reference and can thus be modified.
Tough some more effort is needed to provide the correct return
type of the filter function; I do not see yet any fundamental
obstacle to this approach.

Yours,
        Martin.


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