2

This tutorial says,

You're probably noticing a similarity to pointers here--and that's true, references are often implemented by the compiler writers as pointers

In similar, one commented in

What is a reference variable in C++?

as

Technically not. If bar was a variable you could get its address. A reference is an alias to another variable (not the address of as this would imply the compiler would need to insert a dereference operation). When this gets compiled out bar probably is just replaced by foo

Which statement is true?

Community
  • 1
  • 1
VINOTH ENERGETIC
  • 1,775
  • 4
  • 23
  • 38
  • @JBL you beat me to the edit – sehe Jan 08 '14 at 10:38
  • 3
    Those statements are not contradictory. – Benjamin Lindley Jan 08 '14 at 10:38
  • The hints are the phrases `...are often implemented...` and `...probably is just replaced...` I.e. Both or neither could be the case, depending on how your compiler is implemented. – benjymous Jan 08 '14 at 10:40
  • Both are true, and they aren't at-all contradictory. The key word in the former is "implemented", which should be a signal to you there is no single "way" it is done, and from a user-code perspective you shouldn't care anyway. – WhozCraig Jan 08 '14 at 10:41

3 Answers3

2

Both are true, but under different circumstances.

Semantically, a reference variable just introduces a new name for an object (in the C++ sense of "object").
(There's plenty of confusion around what "variable" and "object" mean, but I think that a "variable" in many other languages is called an "object" in C++, and that's what your second quote refers to as a "variable".)

If this reference isn't stored anywhere or passed as a parameter, it doesn't necessarily have any representation at all (the compiler can just use whatever it refers to instead).

If it is stored (e.g. as a member) or passed as a parameter, the compiler needs to give it a representation, and the most sensible one is to use the address of the object it refers to, which is exactly the same way as pointers are represented.

Note that the standard explicitly says that it it unspecified whether a reference variable has any size at all.

molbdnilo
  • 64,751
  • 3
  • 43
  • 82
  • I don't think it is correct to say either statement is false, regardless of circumstances, since both are tentative. The first says "often", the second says "probably". – Benjamin Lindley Jan 08 '14 at 10:58
  • @molbdnilo is this statement " the compiler needs to give it a representation" means a varaible is created for reference – VINOTH ENERGETIC Jan 08 '14 at 10:59
  • @Vinoth I suspect you're confusing the language (which contains variables) with its implementation (which may contain representations of variables, when necessary). – molbdnilo Jan 08 '14 at 11:19
1

They're both true, in a manner of speaking. Whether a reference gets compiled as a pointer is an implementation detail of the compiler, rather than a part of the C++ standard. Some compilers may use regular pointers, and some may use some other form or aliasing the referenced variable.

Consider the folowing line:

int var = 0;
int &myRef = var;

Compiler "A" may compile myRef as a pointer, and compiler "B" might use some other method for using myRef.

Of course, the same compiler may also compile the reference in different ways depending on the context. For example, in my example above, myRef may get optimized away completely, whereas in contexts where the reference is required to be present (such as a method parameter), it may be compiled to a pointer.

Karl Nicoll
  • 16,090
  • 3
  • 51
  • 65
1

The C++ Standard states, at §8.3.2/4:

It is unspecified whether or not a reference requires storage.

And this non-specification is the main reason why both a pointer implementation and an aliasing implementation are valid implementations.

Therefore, both can be right.

Shoe
  • 74,840
  • 36
  • 166
  • 272