Boost logo

Boost :

From: David Abrahams (dave_at_[hidden])
Date: 2007-08-13 09:25:52


on Mon Aug 13 2007, "Martin Schulz" <Martin.Schulz-AT-synopsys.com> wrote:

> 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.

That's less simple than you make it sound. It could have a very
complicated type.

One good way to deal with this is to make the proxy object return a
constant object. Then it will bind to a T& parameter.

Furthermore, the constness of the proxy should not affect the
constness of its access to elements of x. That essentially means
returning a different proxy type depending on whether the incoming
container is const or not.

-- 
Dave Abrahams
Boost Consulting
http://www.boost-consulting.com
The Astoria Seminar ==> http://www.astoriaseminar.com

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