I'm wondering what happens when I have a function like this:
typedef std::unordered_map<std::string,std::string> stringmap;
stringmap merge (stringmap a,stringmap b)
{
stringmap temp(a); temp.insert(b.begin(),b.end()); return temp;
}
what happens when the function returns?
Is 'temp' copied to a temporary r-value before being destroyed (out-of-scope) and with C++11 possibly NRVO-ptimized (so the copy of 'temp' is directly written to the return destination slot)?
Nothing should be copied. There are two possibilities:
temp
is moved to an object in the caller's scope; ortemp
becomes an alias for an object in the caller's scope. This is known as "return value optimisation".Before 2011 (specifically, without move semantics), the first case would require a copy rather than a move. The second case was allowed in C++98 as well as 11.
NRVO would mean that temp
itself is constructed in the return value location. Without RVO, yes, temp
is copied/moved from its location on the stack into the return value location prior to being destroyed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With