为什么不总是将返回值分配给const reference? [英] Why not always assign return values to const reference?

查看:65
本文介绍了为什么不总是将返回值分配给const reference?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一些功能:

Foo GetFoo(..)
{
  ...
}

假设我们既不知道该函数的实现方式也不知道其内部Foo(例如,它可以是非常复杂的对象)。但是我们确实知道该函数按值返回Foo,并且我们希望将此返回值用作const。

Assume that we neither know how this function is implemented nor the internals of Foo (it can be very complex object, for example). However we do know that function is returning Foo by value and that we want to use this return value as const.

问题:存储return总是一个好主意吗?此函数的值为 const&

Question: Would it be always a good idea to store return value of this function as const &?

const Foo& f = GetFoo(...);

而不是

const Foo f = GetFoo(...);

我知道编译器会做返回值优化,并且可能移动对象而不是复制对象,因此最后的 const& 可能没有任何优势。但是我的问题是,有什么缺点吗?考虑到我不必依赖编译器,为什么我不应该只是开发肌肉记忆以始终使用 const& 来存储返回值优化,甚至对于复杂对象来说,甚至移动操作也可能很昂贵的事实。

I know that compilers would do return value optimizations and may be move the object instead of copying it so in the end const & might not have any advantages. However my question is, are there any disadvantages? Why shouldn't I just develop muscle memory to always use const & to store return values given that I don't have to rely on compiler optimizations and the fact that even move operation can be expensive for complex objects.

将其扩展到极致,为什么我不应该总是 使用 const& 对于我的代码中不可变的所有变量?例如,

Stretching this to extreme, why shouldn't I always use const & for all variables that are immutable in my code? For example,

const int& a = 2;
const int& b = 2;
const int& c = c + d;

除了更加冗长之外,还有什么缺点吗?

Besides being more verbose, are there any disadvantages?

推荐答案

这些在语义上有所不同,如果您要的不是您想要的东西,那么如果您遇到了麻烦。考虑此代码

These have semantic differences and if you ask for something other than you want, you will be in trouble if you get it. Consider this code:

#include <stdio.h>

class Bar
{
    public:
    Bar() { printf ("Bar::Bar\n"); }
    ~Bar() { printf ("Bar::~Bar\n"); }
    Bar(const Bar&) { printf("Bar::Bar(const Bar&)\n"); }
    void baz() const { printf("Bar::Baz\n"); }
};

class Foo
{
    Bar bar;

    public:
    Bar& getBar () { return bar; }
    Foo() { }
};

int main()
{
    printf("This is safe:\n");
    {
        Foo *x = new Foo();
        const Bar y = x->getBar();
        delete x;
        y.baz();
    }
    printf("\nThis is a disaster:\n");
    {
        Foo *x = new Foo();
        const Bar& y = x->getBar();
        delete x;
        y.baz();
    }
    return 0;
}

输出为:

这是安全的:

Bar :: Bar

Bar :: Bar(const Bar&)

Bar :: 〜Bar

Bar :: Baz

Bar ::〜Bar

This is safe:
Bar::Bar
Bar::Bar(const Bar&)
Bar::~Bar
Bar::Baz
Bar::~Bar

这是一场灾难:

Bar :: Bar

Bar ::〜Bar

Bar :: Baz

This is a disaster:
Bar::Bar
Bar::~Bar
Bar::Baz

注意,在销毁 Bar 之后,我们称 Bar :: Baz 。哎呀。

Notice we call Bar::Baz after the Bar is destroyed. Oops.

问问自己想要的东西,这样一来,如果您能得到想要的东西,就不会为所动。

Ask for what you want, that way you're not screwed if you get what you ask for.

这篇关于为什么不总是将返回值分配给const reference?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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