
Lewis Hyatt wrote:
I'm not quite sure I understand the behavior of this simple test of BOOST_FOREACH:
<snip>
TEST 2: RVALUE starting foreach container was constructed container was copied container was destructed container was destructed -------------
So in either case, using BOOST_FOREACH requires one extra copy. Why is this necessary? I guess the macro is making a copy of the argument to avoid evaluating it more than once, but isn't it possible just to bind the temporary to a const reference instead?
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 -- Eric Niebler Boost Consulting www.boost-consulting.com