我应该将引用或副本分配给值返回函数吗? [英] Should I assign a ref or a copy to a value returning function?

查看:98
本文介绍了我应该将引用或副本分配给值返回函数吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有很多返回值的函数:

We have a bunch of value returning functions:

Foo function_1(){
    Foo f;
    // ...
    return f;
}

Bar function_2(){
    Bar b;
    // ...
    return b;
}

Baz function_3(){
    Baz b;
    // ...
    return b;
}

我正在使用它们来创建局部变量的实例:

I'm using them to create instantiations of local variables:

void example(){  
    //...
    const auto foo = function_1();
    //...
    const auto bar = function_2();
    //...
    const auto baz = function_3();
}

但是,我的队友不断要求我将所有实例化为使用&:

However, my teammates keep asking me to convert all my instantiations to use &:

void example(){  
    //...
    const auto& foo = function_1();
    //...
    const auto& bar = function_2();
    //...
    const auto& baz = function_3();
}

执行此操作的原理似乎与以下问题匹配:
为什么不总是将返回值分配给const引用?

The rationale for doing this seems to match the following question:
Why not always assign return values to const reference?

我了解到auto& x =auto x =要求的是两个不同的事物,并且根据该函数的实现,其行为可能有所不同.

I understand that auto& x = and auto x = are asking for two different things, and the behavior might be different based on the implementation of the function.

话虽这么说,但是我在选择对值返回函数进行此操作时是否在寻求有关差异(如果有)的说明?是否保证行为相同?

That being said, I'm looking for clarification about the differences (if any) when choosing to do this on value returning functions? Is the behavior guaranteed to be the same?

如果它是一个返回值的函数,我该如何在const auto&const auto之间做出选择? (大概const在这里没关系吗?).我在《 C ++核心准则》中找不到关于此的任何建议.我希望它不是一个自以为是的问题.

If its a value returning function how do I decide between const auto& vs const auto? (presumably the const doesn't matter here?). I couldn't find any advice about this in the C++ Core Guidelines. I'm hoping its not an opinioned question.

推荐答案

您的同事正在尝试执行编译器的工作,而不是信任它,结果可能会造成悲观. NRVO得到了很好的支持,如果使用值语义编写函数,则NRVO可以删除多个副本.绑定到引用将防止这种情况,因为引用变量将不满足此优化的条件. 一个简单的测试来演示:

Your colleague is trying to do the compiler's job instead of trusting it, and is potentially pessimizing as a result. NRVO is very well supported, and if the functions are written with value semantics, NRVO can elide multiple copies. Binding to a reference will prevent that, since a reference variable will not satisfy the conditions for this optimization. A simple test to demonstrate:

#include <iostream>

struct Test {
    Test() { std::cout << "Created\n"; }
    Test(const Test&) { std::cout << "Copied\n"; }
};

Test foo() {
    Test t;
    return t;
}

Test bar_good() {
    const auto t = foo();
    return t;
}

Test bar_bad() {
    const auto& t = foo();
    return t;
}

int main() {
    const auto good = bar_good(); (void)good;

    std::cout << "===========\n";

    const auto& bad = bar_bad();  (void)bad;
}

哪个给出输出:

Created
===========
Created
Copied

使用值语义时,一个对象总计,但是使用引用时,一个冗余副本.根据复制(甚至移动)的扩展程度,您可能会看到明显的性能差异.

One object total when utilizing value semantics, but a redundant copy when using references. Depending on how expansive the copy (or even move) is, you could see a noticeable performance difference.

这篇关于我应该将引用或副本分配给值返回函数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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