Why does decltype(auto) return a reference here?

2019-01-31 12:58发布

I think (thought) I understand auto. Same about decltype. However, in C++14, one can have some diabolic thing like decltype(auto) as the return type of a function. Consider the following:

decltype(auto) foo()
{
    int m = 1;
    return m;
}

The return type is int, everything makes sense.

However,

decltype(auto) foo()
{
    int m = 1;
    return (m);
}

returns int& (i.e. reference to int).

I have absolutely NO IDEA why this happens, why do these parentheses make any difference at all!? Hope someone can shed some light on this.

PS: I've also tagged with C++ as there are many more people that check the C++ tag than C++14.

1条回答
我想做一个坏孩纸
2楼-- · 2019-01-31 13:20

7.1.6.2 [dcl.type.simple]

  1. For an expression e, the type denoted by decltype(e) is defined as follows:
    — if e is an unparenthesized id-expression or an unparenthesized class member access (5.2.5), decltype(e) is the type of the entity named by e. If there is no such entity, or if e names a set of overloaded functions, the program is ill-formed;
    — otherwise, if e is an xvalue, decltype(e) is T&&, where T is the type of e;
    — otherwise, if e is an lvalue, decltype(e) is T&, where T is the type of e;
    — otherwise, decltype(e) is the type of e.

In your example you have return (m) so e is (m). That is not an unparenthesized id-expression or class member access, so we go to the second bullet. It is not an xvalue so we go to the third bullet. It is an lvalue, so the type is T& where T is int.

查看更多
登录 后发表回答