Reputation: 9212
Looking at references in C++ I noticed that all implementations I looked at used a pointer internally.
Does the C++ Standard guarantee that a reference will use a pointer internally or would it be ok for an implementation to use a more "efficient" solution? (I would currently not see how it could be done "better" because when a new stack frame is created there's not really a bulletproof way to know easily at what offset from the stack base pointer the variable that is being referenced is at because the stack is quite dynamic)
Note: I do understand the difference between a pointer and a reference in C++ (This question has nothing to do with that)
Upvotes: 6
Views: 381
Reputation: 8141
If you mean that a reference requires the compiler to allocate storage for a pointer, then that's unspecified.
§ 8.3.2/4
It is unspecified whether or not a reference requires storage.
EDIT: To record Martin Bonner's comment as a useful, practical note,
[F]or debugging purposes it can be quite useful to know what is going on "under the hood". (E.g. to answer questions like "why hasn't this gone completely off the rails?"). In practise, compilers all implement references as pointers (unless they can optimize the reference completely away).
Upvotes: 10
Reputation: 92301
The standard doesn't say how a reference is implemented, just how it works.
It also doesn't say anything about stack frames, that's another implementation detail.
Upvotes: 5
Reputation: 94409
No, it does not make any guarantees about how references are implemented. The C++ language only defines the semantics of references, not their implementation.
Upvotes: 5