Boost logo

Boost Users :

Subject: [Boost-users] Cpp-Next - slightly OT
From: Robert Jones (robertgbjones_at_[hidden])
Date: 2009-08-20 08:54:57


There's a terrific article about RVO and copy elision by Dave A at

http://cpp-next.com/

If I understand this correctly a decent compiler (say VS2008) will compile
this
sequence

using namespace std;
typedef vector< string > V;
V get_names( );

V sorted(V names)
{
    sort(names);
    V ret;
    swap(ret, names);
    return ret;
}

V v = sorted( get_names( ) );

to generate zero copies - the final value of v will be the actual object
generated by get_names().

Whereas this code sequence

using namespace std;
typedef vector< string > V;
V get_names( );

V sorted(V names)
{
    sort(names);
    V ret;
    swap(ret, names);
    return ret;
}

V tmp = get_names( );
V v = sorted( tmp );

Generates exactly one vector copy operation, at the call site of sorted().

Does that sound right?

- Rob.



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net