Boost logo

Boost :

From: David Abrahams (dave_at_[hidden])
Date: 2007-07-05 20:58:58


on Thu Jul 05 2007, Lewis Hyatt <lhyatt-AT-princeton.edu> wrote:

> cerr << "TEST 2: RVALUE" << endl;
> {
> cerr << "\tstarting foreach" << endl;
> BOOST_FOREACH(int const& i, get_vec(5));
> }
...
> If I compile it with the RVO enabled, I get the following output:
> -------------
> TEST 1: LVALUE
> container was constructed
> starting foreach
> container was destructed
>
> TEST2: LVALUE, REFERENCE
> container was constructed
> starting foreach
> container was destructed
> 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. nt to

I'm not so sure. If you want to avoid seeing the copy that happens
when get_vec returns, just do

                BOOST_FOREACH(int const& i, noisy_vector(5,0));

That's as good an rvalue as any-----^^^^^^^^^^^^^^^^^
and might clarify things for you a bit.

> isn't it possible just to bind
> the temporary to a const reference instead?

That's up to the implementation. At least according to C++98, it's
allowed to make a copy when you do that :(

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