Re:C++: Return value optimization vs. std::move
by nly on Monday July 20, 2015 @11:54AM
(#9916861)
Doing so would require the standard to give std::move special treatment, which sucks. The way it's specified right now let's you write your own std::move and call it rvalue_cast() if you wish.
[ Parent
| on HN ]
Re:C++: Return value optimization vs. std::move
by AnthonyMouse on Monday July 20, 2015 @12:51PM
(#9917274)
> Doing so would require the standard to give std::move special treatment
Does it? Why change the behavior specifically for std::move instead of just allowing any rvalue reference to be elided when possible?
Re:C++: Return value optimization vs. std::move
by kentonv on Monday July 20, 2015 @04:31PM
(#9918849)
To elide a return copy/move, the compiler has to know at compile time exactly which local variable is going to be returned, so that it can arrange to have that local variable constructed directly into the return location, thus avoiding the need to copy/move it later.
So to elide a copy/move from an rvalue reference, the compiler would have to know at compile time that the rvalue reference always points to some particular local variable.
In theory a compiler could look into the inline definition of std::move() and determine that it always returns a reference to its parameter, but that's a rather involved optimization for the spec to require of all implementations.