ChatGPT解决这个技术问题 Extra ChatGPT

Why does std::move prevent RVO (return value optimization)?

In many cases when returning a local from a function, RVO (return value optimization) kicks in. However, I thought that explicitly using std::move would at least enforce moving when RVO does not happen, but that RVO is still applied when possible. However, it seems that this is not the case.

#include "iostream"

class HeavyWeight
{
public:
    HeavyWeight()
    {
        std::cout << "ctor" << std::endl;
    }

    HeavyWeight(const HeavyWeight& other)
    {
        std::cout << "copy" << std::endl;
    }

    HeavyWeight(HeavyWeight&& other)
    {
        std::cout << "move" << std::endl;
    }
};

HeavyWeight MakeHeavy()
{
    HeavyWeight heavy;
    return heavy;
}

int main()
{
    auto heavy = MakeHeavy();
    return 0;
}

I tested this code with VC++11 and GCC 4.71, debug and release (-O2) config. The copy ctor is never called. The move ctor is only called by VC++11 in debug config. Actually, everything seems to be fine with these compilers in particular, but to my knowledge, RVO is optional.

However, if I explicitly use move:

HeavyWeight MakeHeavy()
{
    HeavyWeight heavy;
    return std::move(heavy);
}

the move ctor is always called. So trying to make it "safe" makes it worse.

My questions are:

Why does std::move prevent RVO?

When is it better to "hope for the best" and rely on RVO, and when should I explicitly use std::move? Or, in other words, how can I let the compiler optimization do its work and still enforce move if RVO is not applied?

Why do people still talk about "hope for the best" these days? What kind of compiler are they using that has C++11 support but can't RVO properly?
Copy elision (the mechanism behind RVO) is permitted only under certain, strict conditions. Writing std::move prevents those conditions from being met.
@KerrekSB And these conditions prevented by std::move are...?
@Troy: You're not alone.
@R.MartinhoFernandes: The problem case is the one where behaviour change is permitted, namely omitting copy/move constructor calls. Since the test case by definition must contain side effects, you're limited to optimizations that rely on copy elision and play by the rules.

M
Mgetz

The cases where copy and move elision is allowed is found in section 12.8 §31 of the Standard (version N3690):

When certain criteria are met, an implementation is allowed to omit the copy/move construction of a class object, even if the constructor selected for the copy/move operation and/or the destructor for the object have side effects. In such cases, the implementation treats the source and target of the omitted copy/move operation as simply two different ways of referring to the same object, and the destruction of that object occurs at the later of the times when the two objects would have been destroyed without the optimization. This elision of copy/move operations, called copy elision, is permitted in the following circumstances (which may be combined to eliminate multiple copies):

in a return statement in a function with a class return type, when the expression is the name of a non-volatile automatic object (other than a function or catch-clause parameter) with the same cv-unqualified type as the function return type, the copy/move operation can be omitted by constructing the automatic object directly into the function’s return value

[...]

when a temporary class object that has not been bound to a reference (12.2) would be copied/moved to a class object with the same cv-unqualified type, the copy/move operation can be omitted by constructing the temporary object directly into the target of the omitted copy/move

[...]

(The two cases I left out refer to the case of throwing and catching exception objects which I consider less important for optimization.)

Hence in a return statement copy elision can only occur, if the expression is the name of a local variable. If you write std::move(var), then it is not the name of a variable anymore. Therefore the compiler cannot elide the move, if it should conform to the standard.

Stephan T. Lavavej talked about this at Going Native 2013 (Alternative source) and explained exactly your situation and why to avoid std::move() here. Start watching at minute 38:04. Basically, when returning a local variable of the return type then it is usually treated as an rvalue hence enabling move by default.


This is the answer I was hoping for. I'm not happy with the situation, but I understand better.
We should probably fix is so that return std::move can be elided. If we could tell C++ that the reference return value of a function is guaranteed to be the same as one particular reference input value of the function, a few interesting results could open up. (Elision from non-trivial expressions, lifetime extension of a temporary input argument to a function without returning a temporary, for two: the second of which is IMHO more important).
Yes, I don't understand the rational for not being able to elide this. It is just more difficult to do, allowing compiler writers more time?
@Adrian I mean allowing it is not going to "give them less time". Guaranteeing would. If it's just allowed, they don't have to implement it.
@TomášRůžička, do you mean not allowed?
R
R. Martinho Fernandes

how can I let the compiler optimization do its work and still enforce move if RVO is not applied?

Like this:

HeavyWeight MakeHeavy()
{
    HeavyWeight heavy;
    return heavy;
}

Transforming the return into a move is mandatory.


So returning a local is guaranteed to be a move in worst case, and RVO applies in best case?
I guess I need to do some grep'ping for return std::move ;)
That would actually be a useful warning.
@cdoubleplusgood Almost. Returning a local non-parameter directly is guaranteed to move in the worst case. Something as innocuous as return condition?heavy1:heavy2; may prevent the implicit move, while if (condition) return heavy1; return heavy2; would not. It is somewhat fragile.