c++ - 我们能否在可能的情况下使用返回值优化,而在可能的情况下退回到移动而不是复制语义?

标签 c++ c++11 c++14

是否可以编写 C++ 代码,在可能的情况下依赖返回值优化 (RVO),但在不行的情况下回退到移动语义?例如,下面的代码由于有条件不能使用RVO,所以它把结果复制回来:

#include <iostream>

struct Foo {
    Foo() {
        std::cout << "constructor" << std::endl;
    }
    Foo(Foo && x) {
        std::cout << "move" << std::endl;
    }
    Foo(Foo const & x) {
        std::cout << "copy" << std::endl;
    }
    ~Foo() {
        std::cout << "destructor" << std::endl;
    }
};

Foo f(bool b) {
    Foo x;
    Foo y;
    return b ? x : y;  
}

int main() {
   Foo x(f(true));
   std::cout << "fin" << std::endl;
}

这产生

constructor
constructor
copy
destructor
destructor
fin
destructor

这是有道理的。现在,我可以通过更改行来强制在上面的代码中调用移动构造函数

    return b ? x : y;  

    return std::move(b ? x : y);

这给出了输出

constructor
constructor
move
destructor
destructor
fin
destructor

但是,我不太喜欢直接调用 std::move。

实际上,问题是我处于这样一种情况,即使构造函数存在,我也绝对不能调用复制构造函数。在我的用例中,要复制的内存太多,虽然删除复制构造函数会很好,但出于各种原因,这不是一个选项。同时,我想从函数中返回这些对象并且更愿意使用 RVO。现在,我真的不想在编码时、应用时和不应用时记住 RVO 的所有细微差别。大多数情况下,我希望返回对象而不希望调用复制构造函数。当然,RVO 更好,但移动语义很好。有没有办法在可能的情况下使用 RVO,在没有的情况下使用移动语义?


编辑1

以下question帮助我弄清楚发生了什么。基本上,标准的 12.8.32 规定:

When the criteria for elision of a copy operation are met or would be met save for the fact that the source object is a function parameter, and the object to be copied is designated by an lvalue, overload resolution to select the constructor for the copy is first performed as if the object were designated by an rvalue. If overload resolution fails, or if the type of the first parameter of the selected constructor is not an rvalue reference to the object’s type (possibly cv-qualified), overload resolution is performed again, considering the object as an lvalue. [ Note: This two-stage overload resolution must be performed regardless of whether copy elision will occur. It determines the constructor to be called if elision is not performed, and the selected constructor must be accessible even if the call is elided. —end note ]

好吧,为了弄清楚复制 elison 的标准是什么,我们看看 12.8.31

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 cvunqualified 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

因此,如果我们将 f 的代码定义为:

Foo f(bool b) {
    Foo x;
    Foo y;
    if(b) return x;
    return y;
}

然后,我们的每个返回值都是一个自动对象,因此 12.8.31 表示它符合复制 elison 的条件。这会转到 12.8.32,它表示复制的执行就像它是一个右值一样。现在,RVO 不会发生,因为我们事先不知道要采用哪条路径,但是由于 12.8.32 中的要求,移动构造函数被调用。从技术上讲,复制到 x 时避免了一个移动构造函数。基本上,在运行时,我们得到:

constructor
constructor
move
destructor
destructor
fin
destructor

在构造函数上关闭 elide 生成:

constructor
constructor
move
destructor
destructor
move
destructor
fin
destructor

现在,假设我们回到

Foo f(bool b) {
    Foo x;
    Foo y;
    return b ? x : y;
}

我们必须看看 5.16.4 中条件运算符的语义

If the second and third operands are glvalues of the same value category and have the same type, the result is of that type and value category and it is a bit-field if the second or the third operand is a bit-field, or if both are bit-fields.

因为 x 和 y 都是左值,所以条件运算符是左值,但不是自动对象。因此,12.8.32 不会启动,我们将返回值视为左值而不是右值。这需要调用复制构造函数。因此,我们得到

constructor
constructor
copy
destructor
destructor
fin
destructor

现在,由于本例中的条件运算符基本上是复制值类别,这意味着代码

Foo f(bool b) {
    return b ? Foo() : Foo();
}

将返回一个右值,因为条件运算符的两个分支都是右值。我们看到这一点:

constructor
fin
destructor

如果我们关闭构造函数的省略,我们会看到移动

constructor
move
destructor
move
destructor
fin
destructor

基本上,我们的想法是,如果我们返回一个右值,我们将调用移动构造函数。如果我们返回一个左值,我们将调用复制构造函数。当我们返回一个类型与返回类型匹配的非 volatile 自动对象时,我们返回一个右值。如果我们有一个像样的编译器,这些拷贝和移动可能会被 RVO 省略。但是,至少,我们知道在无法应用 RVO 的情况下调用什么构造函数。

最佳答案

当 return 语句中的表达式是一个非 volatile 自动持续时间对象,而不是函数或 catch 子句参数,具有与函数返回类型相同的 cv-unqualified 类型时,生成的复制/移动符合条件复制省略。该标准还继续说,如果禁止复制省略的唯一原因是源对象是函数参数,并且如果编译器无法省略拷贝,则拷贝的重载决议应该像expression 是一个右值。因此,它更喜欢移动构造函数。

OTOH,因为您使用的是三元表达式,所以所有条件都不成立,您只能使用常规拷贝。将您的代码更改为

if(b)
  return x;
return y;

调用移动构造函数。

请注意,RVO 和复制省略之间存在区别 - 复制省略是标准允许的,而 RVO 是一种常用于在标准允许复制省略的情况下删除拷贝的技术。

关于c++ - 我们能否在可能的情况下使用返回值优化,而在可能的情况下退回到移动而不是复制语义?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27633297/

相关文章:

c++ - 了解新运营商

c++ WINAPI 共享内存结构数组

c++ - 在 C++ 中仅使用堆栈的后果

c++ - shared_ptr::reset 仅适用于原始指针?

c++ - g++ vs intel/clang 参数传递顺序?

c++ - constexpr 静态成员函数用法

c++ - 类型特征检查类特殊函数的琐碎性的基本原理

c++ - 当发生段错误时不调用信号处理程序?

c++ - constexpr exp、log、pow

c++ - 获取 C++ 中表达式的类型,包括引用