Boost logo

Boost :

From: Lewis Hyatt (lhyatt_at_[hidden])
Date: 2007-07-06 10:59:48


> Let me be more precise: using BOOST_FOREACH *with rvalue containers*
> requires one "extra" copy. When the container is an lvalue, no copies
> are made, ever. You suggested binding the rvalue to a const reference.
> That would work, if you knew both that the object was an rvalue *and*
> what the type of the object was. But you don't. Instead, you have to put
> the object in a variant of sorts, and store a const reference to that.
> That's where the extra copy comes from.
>
> For all the details, see: http://www.artima.com/cppsource/foreach.html

OK, thanks, I did read your article first, it's very informative, but I
felt that it never considered the possibility of binding to a const
reference instead of making a copy. I guess, once the auto keyword is
available, then you could probably avoid the copy, although you might
have to make two macros, one that allows the container to be altered,
and one that doesn't, or something like that. Anyway, I guess this isn't
such a big issue, because if copying a container is too expensive, you
probably shouldn't be returning it from functions anyway...

-Lewis


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