I don't understand when I should use std::move
and when I should let the compiler optimize... for example:
using SerialBuffer = vector< unsigned char >;
// let compiler optimize it
SerialBuffer read( size_t size ) const
{
SerialBuffer buffer( size );
read( begin( buffer ), end( buffer ) );
// Return Value Optimization
return buffer;
}
// explicit move
SerialBuffer read( size_t size ) const
{
SerialBuffer buffer( size );
read( begin( buffer ), end( buffer ) );
return move( buffer );
}
Which should I use?
move
explicitly: modern compilers are smart enough to use RVO pretty much everywhere and it's more efficient thanmove
. But that's just "hearsay", mind you, so I'm quite interested in a documented explanation. – Honeysweetstd::unique_ptr<base> f() { auto p = std::make_unique<derived>(); p->foo(); return p; }
, but if the types are the same it will move if possible (and that move might be elided) – Packston