Why does decltype(auto) return a reference here?

前端 未结 1 1559
时光取名叫无心
时光取名叫无心 2020-12-24 01:45

I think (thought) I understand auto. Same about decltype. However, in C++14, one can have some diabolic thing like decltype(auto) as t

相关标签:
1条回答
  • 2020-12-24 02:28

    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.

    0 讨论(0)
提交回复
热议问题