9
int main(){ int ival=1024; int &refVal=ival; int &refVal2=refVal; return 0; } 

C++ Primer(5th edition) says "Because references are not objects, we may not define a reference to a reference."(Chinese 5th version says "不能定义引用的引用".meaning can't define a reference to a reference. )

But I got the code above pass compilation.

What's going on?

Feel free to correct any errors(including my English skills)

2
  • 3
    Illegal in the same sense that driving a rock on the highway is illegal. (that is to say, how do you even do it? Because your code doesn't) Commented Feb 6, 2015 at 6:23
  • 2
    A reference is an alias to the object itself. So when you try to make a reference to a reference, the reference passes it on to the object itself so you end up with just another reference to the object. Commented Feb 6, 2015 at 6:37

5 Answers 5

14

After refVal is initialized, whenever you mention its name, it behaves like the variable ival it refers to---its "referenceness" can no longer be detected (except by decltype). Therefore refVal2 is simply initialized to refer to ival also.

There is no type "reference to reference to int", int&(&).

Sign up to request clarification or add additional context in comments.

Comments

9

"Because references are not objects, we may not define a reference to a reference."

Perhaps they meant to say:

int i = 10; int& ref1 = i; int&& ref2 = ref1; // Not allowed. 

Of course, in C++11, the symbol && is used to define rvalue references.

I think it's more illustrative to compare references and pointers to understand why references to references does not make sense.

enter image description here

2 Comments

I think the code is the OP's, in his attempt to test the veracity of the statement in the book.
No, they're not saying anything about rvalue vs lvalue references. They're saying that there's no such type as a reference to a reference, only to an object or function.
1

Your code does not have a reference to reference.

int &refVal2 

Clearly a reference to integer, is it not?

6 Comments

then what's "a reference to a reference"?
@guangzhi: There isn't such a thing. There is no syntax for it.
what does the author mean? why does he say that now that there isn't such a thing?
@guangzhi: He mean exactly what he says. You can't define a reference to a reference. And he is correct. What I'm saying is that it also happens to be the case that there isn't even a syntax for defining a reference to a reference. So not only can you not do it, but you cannot even try to do it (other than typing random characters into the keyboard and hoping the compiler can guess what you are trying to do).
got it.But there is another question. I thought "may not" suggest it's possible.Did I get the author wrong?@Benjamin Lindley
|
1

[dcl.ref]/p1:

In a declaration T D where D has either of the forms

& attribute-specifier-seq_opt D1 && attribute-specifier-seq_opt D1 

and the type of the identifier in the declaration T D1 is “derived-declarator-type-list T,” then the type of the identifier of D is “derived-declarator-type-list reference to T.”

Hence, if the type of the identifier in the declaration T D1 is "reference to T", then the type of the identifier in the declaration T & D1 would be "reference to reference to T". In other words, to attempt to declare a reference to reference, you'd do something like this:

int & & refref = refVal; 

However, this code is ill-formed because of [dcl.ref]/p5:

There shall be no references to references, no arrays of references, and no pointers to references.

There's a separate rule in the standard that says that if TREF is a typedef for T&, then when you try to do TREF&, instead of failing because of the rule above, the references would collapse so that TREF& actually means T&. A similar collapsing rule applies to decltype(...). This rule does not apply when you are trying to declare a reference to reference directly.

Comments

0

Your code

int &refVal2=refVal; 

is perfectly valid and fine.

Illegal code would be:

int &&refVal2=refVal; 

4 Comments

operator && has nothing to do with reference, hasn't it?
int &&refVal2=refVal; is illegal, but for a different reason that has nothing to do with "reference to reference".
But int &&refVal2 = std::move(refVal); would be legal. What do rvalue references have to do with a question about references to references?
@MikeSeymour Agree. && has nothing relevant to question about references to references.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.