3 ms·
Sounds like a "specification bug" that RVO is not allowed when std::move is used. Or is there a deeper reason for this?
by im3w1l 11y ago
Sounds like a "specification bug" that RVO is not allowed when std::move is used. Or is there a deeper reason for this?
- nly 11y agoDoing 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.
- pcwalton 11y agoWhy is that an important design criterion?
- Guvante 11y agoNot everyone uses the standard libraries for lots of complicated reasons.
- AnthonyMouse 11y ago> 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?
- kentonv 11y agoTo 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.
- plorkyeran 11y agoThere's never a place where RVO could occur where `return std::move(...)` is a sensible thing to write, so it's not actually a problem in practice. If you're returning a local you don't have to explicitly move from it, and if you're returning a global or member variable RVO can't occur.