0
int& f(){
    int x = 5;
    int& y = x;

    return y;
}

Why does the function above work? How is it possible to return a reference of a local variable which will be lost when of scope?

zach
  • 95
  • 1
  • 7
  • It's called Undefined Behavior, it can do everything, even work as expected. Till you try to use it in slightly different circumstances – JVApen Jun 27 '20 at 12:14
  • It does not "work" since C++ does not specify what "work" means in this case. – Daniel Langr Jun 27 '20 at 12:29

0 Answers0