Case 1:
(情况1:)
#include <iostream>
decltype(auto) fun()
{
std::string str = "In fun";
return str;
}
int main()
{
std::cout << fun() << std::endl;
}
Here, program work fine in Gcc Compiler.
(在这里,程序可以在Gcc编译器中正常运行。)
decltype(auto)
is deduced to be the type of str
. (decltype(auto)
被推导为str
的类型。)
Case 2:
(情况2:)
#include <iostream>
decltype(auto) fun()
{
std::string str = "In fun";
return (str); // Why not working??
}
int main()
{
std::cout << fun() << std::endl;
}
Here, generated following error and Segmentation fault :
(在这里,产生以下错误和分段错误 :)
In function 'decltype(auto) fun()':
prog.cc:5:21: warning: reference to local variable 'str' returned [-Wreturn-local-addr]
std::string str = "In fun";
^~~
Segmentation fault
Why does return (str);
(为什么return (str);
)
giving segmentation fault? (给分割错误?)
ask by msc translate from so 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…