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.