No even if the compiler would let it slip, the variable dies with the scope of the function, so you would have a dangling reference and effectively enter Undefined Behaviour state.
Return by value, any descent compiler will perform (N)RVO. There are some restrictions, so you should alaways check the requirements for your compiler. Especially if there are multiple exit points. But it's best course of action and produces cleanest and fastest code.
Addendum:
It does generate ("just") a warning on both clang++
and g++
:
luk32@debianvm:~/projects/tests$ g++ ./dangling_reference.cpp
./dangling_reference.cpp: In function 'std::vector<int>& test()':
./dangling_reference.cpp:6:17: warning: reference to local variable 'v' returned [-Wreturn-local-addr]
vector<int> v(10);
^
luk32@debianvm:~/projects/tests$ clang++ ./dangling_reference.cpp
./dangling_reference.cpp:7:12: warning: reference to stack memory associated with local variable 'v' returned [-Wreturn-stack-address]
return v;
However I would advise to mark those warnings as errors, as they nearly always are (by nearly I mean to be on the safe side, I don't know how to return reference to a local variable and not enter UB), and consider adding appropriate switches to the compiler commands. E.g.:
g++ -Werror=return-local-addr ./dangling_reference.cpp
clang++ -Werror=return-stack-address ./dangling_reference.cpp
IMO, it's a good practice if your project is big enough for anything more than compiling by hand.