返回int&函数 [英] Function returning int&

查看:123
本文介绍了返回int&函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正环顾四周,发现一篇试图解释的文章std :: move 和rvalues ,发现我确实无法掌握的内容。

I was looking around the net and found an article which tries to explain std::move and rvalues and found something I really can't grasp.


int i = 42; 
i = 43; // ok, i is an lvalue 
int* p = &i; // ok, i is an lvalue 
int& foo(); 
foo() = 42; // ok, foo() is an lvalue 
int* p1 = &foo(); // ok, foo() is an lvalue


这是什么意思 int& foo()?在此示例中, foo()= 42 是什么?

What's the point of int& foo()? What is foo() = 42 in this example?

编辑:由于让我感到困惑而没有与右值无关,我将编写解决问题的部分:

Since the part that got me confused and doesn't have anything to do with rvalues I'll write the part that solved my problem:

我不知道您可以在范围内编写函数声明,因此int& foo()成为带有空初始化程序的int引用,这也是不可能的,因为引用必须指向某个东西。但是在此刻我已经吃饱了。……

I didn't know you could write function declarations inside scopes so int& foo() became a int reference with empty initializer which also is impossible since references must point to something. But in the heat of the moment I went full .......

推荐答案

int& foo()是一个返回(lvalue)对整数的引用的函数。

int& foo() is a function returning a (lvalue) reference to an integer.

引用就像一个指针,但与指针不同没有自己的身份。在逻辑上,它是目标的别名,而不是目标的地址。

A reference is like a pointer, but unlike a pointer it has no identity of its own. It is an alias to its target, instead of the address of its target, logically.

通常将引用实现为指针,但是引用的要点是编译器通常可以完全消除它们的存在。用指针执行操作通常会更加困难,因为它们的标识与所引用的标识是分开的。

Often references are implemented as pointers, but the point of references is that often the compiler can remove their existence entirely. Doing so with pointers is often more difficult, because they have an identity separate from what they refer to.

在C ++中无法获取引用的地址。您可以获取包含引用的结构的地址,该地址非常接近,没有区别,但是您无法获取引用本身的地址。

There is no way in C++ to get the address of a reference. You can get the address of a structure containing a reference, which is so close as makes no difference, but you cannot get the address of the reference itself.

int& foo()返回对整数的引用。例如:

int& foo() returns a reference to an integer. As an example:

int x=0, y=0;
bool pick_x=true;
int& foo() { if (pick_x) return x; else return y; }

现在 foo 返回对其中一个的引用 x y 的值,具体取决于 pick_x 的状态。

now foo returns a reference to either of x or y depending on the state of pick_x.

您几乎可以像 x <一样使用返回值 foo / code>(如果!pick_x ,则为 y 。)

You can use the return value of foo almost exactly as if it was x (or y if !pick_x.)

左值和右值来自C的日子。左值是可以在 = left或right 上出现的表达式>符号,而右值是只能在 = 符号的 right 上进行表达的表达式。

lvalue and rvalue come from the days of C. An lvalue is an expression that can go on the left or right of an = sign, and an rvalue is an expression that can only go on the right of an = sign.

在许多语言中,左值和右值可以用语法表示。在C ++中,引用的存在意味着表达式的返回值可以是要分配的有效值,而重载的 operator = 的存在意味着rvalue有时可以是分配给。

In many languages, lvalue and rvalue can be expressed in the grammer. In C++, the existence of reference means that the return value of an expression can be a valid thing to assign to, and the existence of overloaded operator= means that rvalues can sometimes be assigned to. So things get tricky.

在C ++ 11中,添加了左值引用和右值引用。每个拒绝绑定到其他类型的表达式。还有转发参考,主要出现在模板类型推导中。

In C++11, lvalue reference and rvalue references where added. Each refuse to bind to the other type of expression. There are also forwarding references, which mainly occur in template type deduction.

这篇关于返回int&amp;函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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