4 ms·
> Part of the problem with the new move semantics for me, and C++ in general, is figuring out what the compiler is going to do and their interaction with the re
by int_19h 10y ago
> Part of the problem with the new move semantics for me, and C++ in general, is figuring out what the compiler is going to do and their interaction with the return value optimization.
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.
- frankzinger 10y ago> If you return something that can be moved, it will be moved, always. How about the example I gave in my reply to your sibling? https://news.ycombinator.com/item?id=12578621 https://news.ycombinator.com/item?id=12578621
- int_19h 10y agoGood point. Let me correct that: if you return something that doesn't have a name, or is a local variable in the function you're returning from, it will always be moved. Usually it amounts to the same thing, except when non-const lvalue or rvalue references are involved (but it's fairly rare to return via a non-const reference).
- jnordwick 10y agoSo when I said RVO, I mean't NRVO too (but not from an argument passed in).