Thinking of this question Why can I initialize reference member with data member before initializing the data member? as well as this Why is initialization of a new variable by itself valid? following strange code came to my mind:
int main()
{
int &ri = ri;
ri = 0;
}
This code compiles live demo, I know this is UB, but just wonder, what happens inside compiler, where this reference actually points to?
Update: different situation happens when that reference is global: godbolt, gcc seems to declare it as a pointer and make it to point to itself
Note for downvoters: I know this code is invalid and I am not expecting particular behavior from a compiler, but I think looking inside may help to understand why following code is not illegal and how references work in the language:
struct foo {
int &ri = i;
int i = 0;
};
or this:
extern int i;
int &ri = i;
int i = 0;
Update2: while assignment is definitely UB, it is a good question, if declaration itself:
int &ri = ri;
is UB or not. This seem to be is pure evil - ri
cannot be used in any way, but declaration itself seems to be vaild.