c++ - No compiler warning for returning a reference to local variable -
using:
g++ -wall -ansi foo.cpp
i warning
foo.cpp:31: warning: reference local variable ‘x’ returned
from function :
int &bar(int x) { return x; }
but, removing function file, no warning following function:
int &get_max(int x, int y) { return x > y ? x : y; }
why compiler allow this?
it looks bug, warning inconsistent, if turn on optimization in gcc 5.1 catch case:
warning: function may return address of local variable [-wreturn-local-addr] return x > y ? x : y; ^
while without optimization gcc misses it.
so best thing file bug report. if don't believe bug or won't fix @ least there reference others having same issue.
Comments
Post a Comment