0

When we declare a reference variables int & x = something, is the & just a syntactical shorthand in the source code for referencing the value of the variable something? What I mean is, does the compiler insert, under the hood, the necessary calls for pointers in order to access the value of something, or does it use some more direct approach? E.g. do we have that int x & = something is equal to int* x = &something, and using the value at x like x = 10 results in the code *x = 10?

bernatj
  • 97
  • 5
Epsilon Away
  • 343
  • 2
  • 9
  • 2
    It depends on the exact case. The C++ standard says a reference does not require any storage so it can just be an alias to the referred to object and it is that object that is accessed directly. Only way to know for sure is to check the assembly. – NathanOliver Jun 29 '21 at 13:31
  • 1
    you are mixing what a reference is, how it is specified ("is the & just a syntactical shorthand in the source code for referencing the value of the variable something"-yes kind of) and how that is implemented by compilers. Thats two different questions. References arent pointers, but the implementation of references typically does use pointers – 463035818_is_not_an_ai Jun 29 '21 at 13:31
  • 1
    Best to think of a reference as an alias for an object. Like how a `typedef` is an alias for a type. – Eljay Jun 29 '21 at 14:17

0 Answers0