Why does “return (str);” deduce a different type than “return str;” in C++?

筅森魡賤 提交于 2019-12-20 18:30:03

问题


Case 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. decltype(auto) is deduced to be the type of str.

Case 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); giving segmentation fault?


回答1:


decltype works in two different ways; when using with unparenthesized id-expression, it yields the exact type how it's declared (in case 1 it's std::string). Otherwise,

If the argument is any other expression of type T, and

a) if the value category of expression is xvalue, then decltype yields T&&;

b) if the value category of expression is lvalue, then decltype yields T&;

c) if the value category of expression is prvalue, then decltype yields T.

and

Note that if the name of an object is parenthesized, it is treated as an ordinary lvalue expression, thus decltype(x) and decltype((x)) are often different types.

(str) is a parenthesized expression, and it's an lvalue; then it yields the type of string&. So you're returning a reference to local variable, it'll always be dangled. Dereference on it leads to UB.



来源:https://stackoverflow.com/questions/50674046/why-does-return-str-deduce-a-different-type-than-return-str-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!