My understanding is that a const T&
is internally implemented via a const T *const
. If so, how is the below code valid (since we cannot take the address of 7)?
const int& i = 7;
Or does the compiler does some trick here? If so, what?
My understanding is that a const T&
is internally implemented via a const T *const
. If so, how is the below code valid (since we cannot take the address of 7)?
const int& i = 7;
Or does the compiler does some trick here? If so, what?
Copyright © 2021 Jogjafile Inc.
You cannot take the address of a literal like
7
but you can take the address of a temporary object whose value is7
.Think of that line to be equivalent to
The compiler does something analogous to the two lines I mentioned but it is not a trick. It is required by the language.
Searching for "extending the lifetime of temporaries" in SO resulted in a long list.