4 ms·
Why does adding the std::move() change the generated code though? It changes the prvalue to an xvalue, but std::move is equivalent to a static_cast, and shouldn
by lilyball 6y ago
Why does adding the std::move() change the generated code though? It changes the prvalue to an xvalue, but std::move is equivalent to a static_cast, and shouldn’t both the prvalue and the xvalue versions end up calling the same std::array move constructor?
Edit: I just took a look at the actual godbolt links. And it looks like the std::move version actually puts a call to std::move in the generated assembly. I’m kind of surprised this is actually a literal function call, I would have assumed std::move would be always inlined.
- rrss 6y ago> I’m kind of surprised this is actually a literal function call, I would have assumed std::move would be always inlined The godbolt links are without any optimization.
- edflsafoiewq 6y agoThe version without std::move does not call a constructor because of copy elision. The get_display_pixels function constructs the disp_pixels object in place. In the version with std::move, it first copy-elides the function call into a temporary, then move-constructs the disp_pixels object from the temporary. ie as if auto tmp = emulator.get_display_pixels(); const auto disp_pixels = std::move(tmp); // move constructor called here // lifetime of tmp ends So the version with std::move is invoking an extra move-constructor and destructor call.
- iainmerrick 6y agoBut shouldn’t those extra calls be optimized away to nothing, if they don’t have any real side-effects? That’s what I would naively assume.
- edflsafoiewq 6y agoI expect so, yes.
- aw1621107 6y agoI think the calls to std::move in those specific examples are due to optimization flags not being passed to the compiler invocations on godbolt. That being said, I've definitely heard of std::move in the wrong places preventing optimizations (e.g., std::move on a return statement can (will?) inhibit copy elison), but I'm not familiar enough with the finer details of C++ be able to tell you why.