https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109945
--- Comment #10 from Andrew Pinski <pinskia at gcc dot gnu.org> --- (In reply to Andrew Pinski from comment #9) > (In reply to Andrew Pinski from comment #8) > > (In reply to Arthur O'Dwyer from comment #7) > But does w go out of the scope at the end of make2? Similar question to make > in the original testcase, does the temp go out of scope? For both of these testcases GCC currently warns even: <source>: In function 'Widget make2()': <source>:6:35: warning: storing the address of local variable '<return value> w' in 'global' [-Wdangling-pointer=] 6 | Widget make2() { Widget w; global = &w; return w; } | ~~~~~~~^~~~ <source>:6:1: note: '<return value> w' declared here 6 | Widget make2() { Widget w; global = &w; return w; } | ^~~~~~ <source>:5:9: note: 'global' declared here 5 | Widget *global = nullptr; | ^~~~~~ So Maybe this is undefined code after all.