 <p>Think about what the compiler would have to do to catch the problem you're demonstrating. It would have to look at all callers of test1 to see whether they're passing it a local. Perhaps easy enough, but what if you insert more and more intermediate functions?</p> <pre><code>int &amp; test1(int &amp; x) { return x; } int &amp; test2(int &amp; x) { return test1(x); } int &amp; test3() { int x = 0; return test2(x); } int main(void){ int &amp; r = test3(); return r; } </code></pre> <p>The compiler would have to look not only at all callers of test1, but then also all callers of test2. It would also have to work through test2 (imagine that it's more complex than the example here) to see whether it's passing any of its own locals to test1. Extrapolate that to a truly complex piece of code--keeping track of that sort of thing would be prohibitively complex. The compiler can only do so much to protect us from ourselves.</p>