在变量中存储返回之前的值 [英] Storing value before return in a variable

查看:119
本文介绍了在变量中存储返回之前的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是一段代码:

bool EqualsA(const Foo& a, const Foo& b)
{
    return a == b;
}

bool EqualsB(const Foo& a, const Foo& b)
{
    const bool result = a == b;

    return result;
}

int MethodA()
{
    return GetValue() * GetOtherValue();
}

int MethodB()
{
    const int result = GetValue() * GetOtherValue();

    return result;
}



我想知道这两种不同的返回值是否有任何差异方法(即时返回或存储结果在变量中)。我认为存储更适合调试,但是有任何性能损失(我不认为有)或任何其他利弊使用其中的优点和缺点。

I wanted to know if there is any difference in returning values in these two different ways (instant return or store result in a variable). I think that storing is better for debugging but is there any performance loss (I don't think there is) or any other pros and cons for using one of those.

推荐答案

编译器可以自由优化局部变量,因此性能将是相同的。

The compiler is free to optimize away the local variable so the performance is going to be the same.

在许多代码分析工具中,这被标记为代码气味,我倾向于同意。 调试器可以查看堆栈上的返回值< a>所以局部变量不买任何东西。

In a lot of code analysis tools this is marked as a code smell and I would tend to agree. Debuggers can be made to see the return value on the stack so the local variable doesn't buy anything.

这篇关于在变量中存储返回之前的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆