【问题标题】:Do I have to rely on compiler NRVO?我必须依赖编译器 NRVO 吗?
【发布时间】:2014-12-10 21:43:29
【问题描述】:

我需要调用一个函数来为我返回一个对象。问题是该对象有一个析构函数,它可以在函数输出被分配给另一个对象之前破坏数据。 在我的程序中,我有一个 operator+,它将两个矩阵相加并返回两个矩阵的总和:

C=A+B

根据名称返回值优化(NRVO),以下代码不应立即调用析构函数:

Matrix operator+(const Matrix &m1, const Matrix &m2)
{
    Matrix sum;
    m1.clone(sum);
    for...
        for...
            sum.members[i][j]+=m2.members[i][j];
    return sum;
}

我的问题是我对信任 NRVO 没有信心,因为它取决于编译器。如果我将代码提供给其他人,他可能会编译代码,而他的编译器会给出不同的结果。

那么,有没有办法强制编译器给我我真正需要的东西,或者我必须将我的代码更改为如下不受欢迎的形式?

Matrix sum(Matrix &result, const Matrix &m1, const Matrix &m2)

编辑:

为了解释更多,我假设通过考虑 NRVO,问题运行如下:

compiler reaches to C=A+B
operator + is called
object sum is created
object sum is calculated as sum of m1 and m2
sum is returned but its destructor is not called
the value of sum is directed to variable C
after function containing variable C reaches end, the destructor of C is called.

当未应用 NRVO 时,我希望:

compiler reaches to C=A+B
operator + is called
object sum is created
object sum is calculated as sum of m1 and m2
sum is returned and its destructor is called which releases all data allocations
the return value of the operator+ is already destroyed so an invalid data is associated to variable C
...

【问题讨论】:

  • 如果你的班级按照你说的做,你需要修复它。
  • 您编写的代码不应依赖特定的编译器优化才能正常运行。
  • 听起来您需要阅读有关对象生命周期的信息。请指出你的代码中你期望析构函数调用的位置,我们应该能够解决这个问题。
  • @RobertHarvey 这正是我的问题。无论如何要在不失去 OO 操作员优势的情况下修复它?
  • 您认为哪些物品可能会被提前收集?

标签: c++ optimization compiler-optimization


【解决方案1】:

问题是对象有一个析构函数,它可以在函数输出被分配给另一个对象之前破坏数据。

这不是问题。对象要么被正确复制,要么通过优化消除不必要的复制。如果您的复制 ctor 正确实施,最终结果将是相同的(当然除了不太理想的代码)。如果对象复制过于昂贵,您可能应该在写入语义上使用复制,而实际的 Matrix 对象将是在堆上创建的真实对象的精简包装器。

不应用 NRVO 时实际会发生什么:

compiler reaches to C=A+B
operator + is called
object sum is created
object sum is calculated as sum of m1 and m2
temporary object of type Matrix created as a copy of object sum
object sum destroyed
temporary assigned to C

如您所见,最终结果是相同的,只是效果较差(创建了临时对象)

【讨论】:

    猜你喜欢
    • 2021-10-03
    • 1970-01-01
    • 1970-01-01
    • 2021-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-09
    • 2011-02-04
    相关资源
    最近更新 更多