Skip to main content
edited tags
Link
The Thonnu
  • 1.6k
  • 2
  • 6
  • 31
Source Link
CPlus
  • 9.2k
  • 2
  • 33
  • 96

How are C++ style references implemented behind the scenes? Could they be implemented without pointers?

I would not be surprised if C++ style int &y = x; was no more than int *y = &x; behind the scenes and every use of y was implicitly *y, and that this would this incur the computational expense of pointer indirection. And since the pointer is being stored anyway, there would be no memory-saving benefits here.

However, would implementing C++ style references in a manner that was direct, and more efficient than just using pointers behind the scenes, such as y accessing x on the stack directly the same way simply writing x does (besides a preprocessor macro) be possible? The same way x refers to a certain register at translation time, y could be made to refer to the same register. If there is a struct and a reference is declared to point to the member, could the reference just be made to refer to the register member at translation time without pointers? What would implementing this take?