Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reference of Reference in C++

Tags:

c++

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

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.

like image 336
user541686 Avatar asked Apr 08 '11 05:04

user541686


People also ask

What is a reference in C language?

The C Language Reference describes the C programming language as implemented in Microsoft C. The book's organization is based on the ANSI C standard (sometimes referred to as C89) with additional material on the Microsoft extensions to the ANSI C standard. Organization of the C Language Reference.

Can you have a reference to a reference?

Citing a source that you found in another source is known as using a secondary source. You should always try to read and cite the original work (the primary source). If it is not possible to do this, you have to cite the original as contained in the secondary source.

What is pass by reference in C?

Passing by by reference refers to a method of passing the address of an argument in the calling function to a corresponding parameter in the called function. In C, the corresponding parameter in the called function must be declared as a pointer type.

Is reference to a reference allowed in C++?

C++ references differ from pointers in several essential ways: It is not possible to refer directly to a reference object after it is defined; any occurrence of its name refers directly to the object it references. Once a reference is created, it cannot be later made to reference another object; it cannot be reseated.


2 Answers

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

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 
like image 120
Skurmedel Avatar answered Sep 23 '22 17:09

Skurmedel


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.

like image 35
James McNellis Avatar answered Sep 25 '22 17:09

James McNellis