简体   繁体   English

具有多个收益的RVO运营商

[英]RVO Operators With Multiple Returns

We're working with RVO in class to show how we can reduce the number of temporaries created. 我们正在课堂上与RVO合作,展示如何减少创建的临时工数量。

I get the basics of it but I'm having difficulty understanding how to combine multiple values to return into one line. 我了解了它的基础知识,但是我很难理解如何组合多个值以返回到一行。

For single temp optimization I was able to understand it fairly easy 对于单温度优化,我很容易理解

const A operator + ( const A &tmp)
{
    A sum;
    sum = this->x + tmp.x;
    return sum;
}

can be reduced to: 可以简化为:

const A operator + ( const A &tmp)
{
    return A(this->x + tmp.x);
}

However I'm uncertain how to apply this to a function with multiple values to return. 但是我不确定如何将其应用于具有多个值的函数以返回。 For example: 例如:

Vect2D operator - ( const Vect2D &tmp ) const;

Vect2D Vect2D::operator - ( const Vect2D &tmp ) const
{
    Vect2D sum;

    sum.x = this->x - tmp.x;
    sum.y = this->y - tmp.y;

    return ( sum );
};

My process behind it would be: 我背后的过程是:

Vect2D Vect2D::operator - ( const Vect2D &tmp ) const
{
    return Vect2D((this->x - tmp.x), (this->y - tmp.y));
};

which comes back with an error telling me " no argument takes the value (float, float) ". 它返回一个错误,告诉我“ no argument takes the value (float, float) ”。

would I have to reorganize the initial 我必须重新组织最初的

Vect2D operator - ( const Vect2D &tmp ) const; Vect2D运算符-(const Vect2D&tmp)const;

to take two float arguments within? 接受两个float参数? or am I thinking about this in the wrong way? 还是我以错误的方式思考这个问题?

Thank you, 谢谢,

E: Thanks to Matt and Jerry for affirming what I thought I needed to do with the double float operator. E:感谢Matt和Jerry肯定了我认为我需要对double float运算符进行的操作。

The solution to my issue was not understanding that operators require the Friend function in order to have two arguments. 解决我的问题的方法是不理解运算符需要Friend函数才能具有两个参数。 While not entirely RVO the solution does reduce the number of temps created by having a return with both variables in it. 尽管不完全是RVO,但该解决方案确实减少了通过同时包含两个变量的返回而创建的临时数量。

Thanks again to Matt and Jerry for getting me started on the process. 再次感谢Matt和Jerry让我着手进行此过程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM