As far as I understand (see this post), C++ references implementation is based on pointers. However, I wonder how the mechanism work when no actual variable is passed, but a literal constant. Let me illustrate what I mean with an example:
#include <iostream>
void f(const int& n)
{
std::cout << n << std::endl;
}
int main()
{
f(42);
}
What I'm passing to the f()
function is literal 42
. The caller is not using any actual variable.
Maybe a "shadowed" on-the-fly variable and associated pointer is used in this case? Anybody can confirm (or explain the actual mechanism if I'm wrong)?
As reference, in the case the answer would be compiler-dependant, we typically use gcc 4.4 (in CentOS 6.x) and gcc 4.9 (in Debian 8).
Note: I already know that it is not a good idea to pass by reference a constant integer. I mean, a better approach fo the f()
function above will be pass-by-value with void f(int n)
. However I think is a good example to illustrate what my doubt.