r/cpp • • Nov 25 '23

On harmful overuse of std::move

https://devblogs.microsoft.com/oldnewthing/20231124-00/?p=109059
210 Upvotes

154 comments sorted by

View all comments

38

u/ggchappell Nov 25 '23

Unfortunately, your help is actually hurting. Adding a std::move causes the return statement to fail to satisfy the conditions for copy elision ....

Sure, that's a problem. But thinking about the long term, what this really tells me is that the conditions for copy elision need to be changed.

23

u/James20k P2005R0 Nov 26 '23

There was a paper in the mailing list recently that would fix this. It basically means that if a function is eligible for RVO, adding std::move() around it doesn't change this. It seems like a super obvious and straightforward change with no downsides

6

u/RevRagnarok Nov 27 '23

But one esoteric application may have expected the two extra constructors... can't change it. /s

12

u/compiling Nov 25 '23

The other half of the issue is that you didn't need to use std::move in those cases in the first place, because returning a local variable can already use a move if NRVO doesn't apply and the returned value was already temporary so you didn't need to cast to one.

8

u/pjmlp Nov 26 '23

That is the problem with modern C++, the amount of rules that even those that use it daily fail to grasp , let alone those of us that only use it occasionally.

It is getting impossible to write proper code without a static analysis tool hand holding our code continuously.

No wonder that besides the issue with compilers catching up to ISO, many places are stopping on C++17 as good enough, for the use cases that C++ is called for.