c++c++14decltypereturn-type-deduction

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


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?


Solution

  • 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's always dangling. Dereference on it leads to UB.