Why do compilers give a warning about returning a reference to a local stack variable if it is undefined behaviour?

前端 未结 7 2153
温柔的废话
温柔的废话 2021-01-01 12:13

The C++ standard states that returning reference to a local variable (on the stack) is undefined behaviour, so why do many (if not all) of the current compilers only

7条回答
  •  盖世英雄少女心
    2021-01-01 12:41

    You could also return a reference to a static variable, which would be valid code so the code must be able to compile.

提交回复
热议问题