C++中的Reference

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5590978/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 18:27:53  来源:igfitidea点击:

Reference of Reference in C++

c++

提问by user541686

I see code on StackOverflow every once in a while, asking about some overload ambiguity with something involving a function like:

我每隔一段时间就会看到 StackOverflow 上的代码,询问一些涉及函数的重载歧义,例如:

void foo(int&& param);

My question is: Why does this even come up? Or rather, when would you ever have "a reference to a reference"? How is that any different from a plain old reference? I've never run across this in real-world code, so I'm curious as to what kind of code would need this.

我的问题是:为什么会出现这种情况?或者更确切地说,你什么时候会有“对参考的参考”?这与普通的旧参考有什么不同?我从来没有在现实世界的代码中遇到过这个,所以我很好奇什么样的代码需要这个。

采纳答案by Skurmedel

It's a rvalue reference, Bjarne describes it here.

这是一个右值引用,Bjarne在此处进行了描述。

Shameless copying ("quoting"):

无耻抄袭(“引用”):

The rvalue reference

An rvalue reference is a compound type very similar to C++'s traditional reference. To better distinguish these two types, we refer to a traditional C++ reference as an lvalue reference. When the term reference is used, it refers to both kinds of reference: lvalue reference and rvalue reference.

An lvalue reference is formed by placing an & after some type.

A a; A& a_ref1 = a;  // an lvalue reference

An rvalue reference is formed by placing an && after some type.

A a; A&& a_ref2 = a;  // an rvalue reference

An rvalue reference behaves just like an lvalue reference except that it can bind to a temporary (an rvalue), whereas you can not bind a (non const) lvalue reference to an rvalue.

A&  a_ref3 = A();  // Error! 
A&& a_ref4 = A();  // Ok

右值参考

右值引用是一种与 C++ 的传统引用非常相似的复合类型。为了更好地区分这两种类型,我们将传统的 C++ 引用称为左值引用。当使用术语引用时,它指代两种引用:左值引用和右值引用。

左值引用是通过在某种类型之后放置一个 & 来形成的。

A a; A& a_ref1 = a;  // an lvalue reference

右值引用是通过在某种类型之后放置 && 来形成的。

A a; A&& a_ref2 = a;  // an rvalue reference

右值引用的行为就像左值引用,除了它可以绑定到临时(右值),而您不能将(非常量)左值引用绑定到右值。

A&  a_ref3 = A();  // Error! 
A&& a_ref4 = A();  // Ok

回答by James McNellis

It isn't a reference to a reference: such a thing does not exist.

它不是对参考的引用:这样的东西不存在。

It is an rvalue reference, a new feature added in the soon-to-be-standardized C++11.

它是一个rvalue reference,是即将标准化的 C++11 中添加的一项新功能。

回答by Potatoswatter

It's an rvalue reference. Note that the &&token used for Boolean AND and rvalue references, and the &token used for bitwise AND and normal references, are different "words" as far as the language can tell.

这是一个右值引用。请注意,就语言而言,&&用于布尔 AND 和右值引用的&标记以及用于按位 AND 和普通引用的标记是不同的“词”。

An rvalue reference is (usually) bound to an object which may be left in an indeterminate state after the rvalue reference is finished, presumably because the object will then be destroyed.

右值引用(通常)绑定到一个对象,该对象在右值引用完成后可能处于不确定状态,大概是因为该对象随后将被销毁。

Simply put, binding a variable to an rvalue reference is the usually the last thing you do to it.

简而言之,将变量绑定到右值引用通常是您对它做的最后一件事。

Unlike a regular reference but like a const &reference, an rvalue reference can bind to an rvalue (an expression whose address cannot be directly taken). Like a regular reference but unlike a const &reference, an rvalue reference can be used to modify its object.

与常规引用不同但与引用类似const &,右值引用可以绑定到右值(无法直接获取地址的表达式)。与常规引用类似但与引用不同的是const &,右值引用可用于修改其对象。