I'm trying to understand why the second piece of code compiles fine, given that the first doesn't.
int & test(void) {
    int v = 0;
    return v;
}
int main(void){
    int & r = test();
    return 0;
}
I understand that this doesn't work because you can't pass a reference to an automatic variable that will be deleted. It seems to me that the code below should have the same problem but it doesn't.
int & test1(int & x) {
    return x;
}
int & test2(void) {
    int x = 0;
    return test1(x);
}
int main(void){
    int & r = test2();
    return 0;
}
Seems like the intermediate function is solving the problem. But why?
 
     
     
     
    