11

I think it's illegal to pass a reference to a reference in C++.However ,when I run this code it gives me no error.

void g(int& y)
{
   std::cout << y;
   y++;
 }

 void f(int& x)
{
  g(x);
}
int  main()
{
  int a = 34;
  f(a);
  return 0;

 }

Doesn't the formal parameter of g() qualify as a reference to a reference ??

devsaw
  • 1,007
  • 2
  • 14
  • 28

5 Answers5

6

No, g() is not taking a reference to reference. It takes a reference to an int. f() forwards the reference to int it receives to g().

A "reference to a reference" doesn't actually exist, but there are rvalue references, which are like references, but allow binding to temporaries.

Community
  • 1
  • 1
jxh
  • 69,070
  • 8
  • 110
  • 193
5

In the body of f, the value of the expression x is an int. The fact that the variable x has type int & means that the value of the expression is an lvalue, and thus it can bind to the parameter of the function g.

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
5

1) There is nothing wrong with passing a reference to a reference (it is what the move-constructor and move-assignment operators use - though it is actually called a rvalue-reference).

2) What you are doing is not passing a reference to a reference, but rather passing the same reference through f to g:

void g(int& x)
{
    x = 5;
}

void f(int& x)
{
    std::cout << "f-in " << x << std::endl;
    g(x);
    std::cout << "f-out " << x << std::endl;
}

int main()
{
    int x = 42;
    f(x);
    std::cout << "New x = " << x << std::endl;
}
Zac Howland
  • 15,777
  • 1
  • 26
  • 42
  • 3
    Move constructor and move assignment do not use reference-to-a-reference. The syntax for their argument is `T&&`, but that's **defined** as an rvalue reference. Reference to reference always has been, and continues to be, illegal. – Pete Becker Aug 27 '13 at 19:28
  • @PeteBecker: If you followed his logic, he was assuming that `int&&` was a reference to a reference (and also assuming that chaining 2 functions that both took `int&` as a parameter would be such). Hence the clarification I placed on #1 followed by further explanation of #2. – Zac Howland Aug 27 '13 at 19:39
  • @PeteBecker: Reading for Comprehension 101 - that is exactly what I said. – Zac Howland Aug 27 '13 at 21:02
  • 1
    "There is nothing wrong with flying a car, but it's called driving". – Pete Becker Aug 27 '13 at 21:05
  • You must be one of those people who reads the specification and gives the client exactly what they said they want instead of what they described wanting ... – Zac Howland Aug 27 '13 at 21:10
  • No, I'm one of those people who knows how to write precise, accurate English. – Pete Becker Aug 27 '13 at 21:12
  • 1
    @PeteBecker: "**though it is actually called a rvalue-reference**" - I'm not sure how much more precise and accurate you can be there. – Zac Howland Aug 28 '13 at 13:12
1

A reference is an alias to a different object. Once the reference has been initialized, it behaves exactly as if you were accessing the object directly, so you are not passing a reference to a reference, but rather a reference to the real object (to which you refer by another reference).

Creating a reference to a reference would be something like:

typedef int& intr;
void f(intr& x);    // reference to a reference type
David Rodríguez - dribeas
  • 204,818
  • 23
  • 294
  • 489
0

Nowhere in your code you attempt to pass a reference to a reference. Inside f expression x produces an lvalue of type int. It is not a reference. Expressions in C++ never produce accessible results of reference type, since any results of reference type are immediately interpreted by the language as lvalues of non-reference type.

See 5/5

If an expression initially has the type “reference to T” (8.3.2, 8.5.3), the type is adjusted to T prior to any further analysis. The expression designates the object or function denoted by the reference, and the expression is an lvalue or an xvalue, depending on the expression.

P.S. I'm not sure what you mean by "Doesn't the formal parameter of g() qualify as a reference to a reference". The formal parameter of g is declared as int &. Where do you see "a reference to a reference" here?

AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765