在linux+gcc下,关于以下代码,正确的是()
std::string& test_str(){ std::string str = "test"; return str; } int main(){ std::string& str_ref = test_str(); std::cout << str_ref << std::endl; return 0; }
test_string_reference.cpp: In function ‘std::string& test_str()’: test_string_reference.cpp:5:11: warning: reference to local variable ‘str’ returned [-Wreturn-local-addr] 5 | return str; | ^~~ test_string_reference.cpp:4:16: note: declared here 4 | std::string str = "test"; | ^~~