C++ has definitely blown it's complexity budget and the problem is getting worse. I don't really see a way out of it either. I sure hope Rust starts to gel or a better C++ comes along, because I don't think I can keep pace with C++ too much longer.
C++ has definitely blown it's complexity budget and the problem is getting worse. I don't really see a way out of it either. I sure hope Rust starts to gel or a better C++ comes along, because I don't think I can keep pace with C++ too much longer.
The rule of thumb is don't use std::move on a return value unless you are returning a move-only type (which forces you to do so).
RVO will be automatically applied if the returned variable is local to a function and of the same type as the function's return type. That's all that the standard allows.
RVO is not applied to function arguments, regardless of whether or not they're rvalue references.
So your rule of thumb breaks for cases such as these (X is a movable and copyable type):
X f(X&& x) {
return x; // x is copied, not moved
}
In order to get x moved into the return location, you would need to do move it explicitly.Source: Effective Modern C++ by Meyers.
https://github.com/llvm-mirror/clang/blob/master/include/cla...
I had a long back and forth relationship with C++. I have the same criticism everyone has: it is hard to use, hard to understand, there are tons of possibilities and concepts that overlap each others, many ways to do the same things that are sometimes incompatible...
But reading more about it, getting updated on the more recent iterations, I understood that C++ is more than a programming language: it is actually a quest. It is a search for a language that can both manipulate high-level concepts and also descend to the very low level required for software living close to the hardware (you'll do mostly C there but a few C++ constructs can be surprisingly useful)
Most other languages will specialize in a niche. C++ developers want to be able to express the most abstract concepts in the best possible implementation. That's a quest for the grail. Few other languages dare to dream that big. And of course C++ stumbled sometime along the way, still limping from backward compatibility, but it is so far the most advanced in this quest.
It's very simple:
If you return something that can be moved, it will be moved, always.
[N]RVO is something that may or may not happen. However, if you don't write code that does crazy things (like auto_ptr style copy constructor that actually moves), at most, it'll result in an extra copy/move that is redundant and won't change semantics. So you should never care about whether it happens or not.
How about the example I gave in my reply to your sibling? https://news.ycombinator.com/item?id=12578621
I agree it's inconvenient, ideally a compiler should do the right thing, but there are no such smart compilers yet :)
Not meant to offend you, but that should not be any of your business :-)
There are languages in which not knowing what the tools are going to do is never a concern. C++ isn't one of those languages.
Summing up, knowing your tool is a must, but if you find yourself doing its job, than it's time to at least question your choice.
Stop using C++ unless forced to do so.
That's the only way to leave the complexity minefield.