What expressions yield a reference type when decltype is applied to them?

后端 未结 2 1655
星月不相逢
星月不相逢 2020-11-27 02:26

I was reading C++ Primer and couldn\'t quite understand when an expression yields an object type, and when it yields a reference type to the object.

I quote from the

2条回答
  •  清歌不尽
    2020-11-27 03:10

    For expressions, as in your examples decltype will provide a reference type if the argument is lvalue.

    7.1.6.2p4:

    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.
    The operand of the decltype specifier is an unevaluated operand (Clause 5).
    [ Example:
    const int&& foo();
    int i;
    struct A { double x; };
    const A* a = new A();
    decltype(foo()) x1 = i; // type is const int&&
    decltype(i) x2; // type is int
    decltype(a->x) x3; // type is double
    decltype((a->x)) x4 = x3; // type is const double&
    —end example ]
    

提交回复
热议问题