What rvalues have names?

2019-06-15 03:05发布

问题:

@FredOverflow mentioned in the C++ chatroom that this is a rare case of rvalues that have names. The C++0x FDIS mentions under 5.1.1 [expr.prim.general] p4:

Otherwise, if a member-declarator declares a non-static data member (9.2) of a class X, the expression this is a prvalue of type “pointer to X” within the optional brace-or-equal-initializer. It shall not appear elsewhere in the member-declarator. (emphasis mine)

What others are there, if any?

回答1:

One prominent case are enumerators

enum arity { one, two };

The expressions one and two are rvalues (more specifically, prvalues in C++0x). Another are template non-type parameters

template<int *P> struct A { };

The expression P is an rvalue too (more specifically again, a prvalue in C++0x).



回答2:

  1. The boolean literals true and false are prvalues of type bool.
  2. nullptr is a prvalue of type nullptr_t.
  3. When you return a named variable from a function, it becomes an xvalue in the context of that expression, and an xvalue is an rvalue (per §3.10/1).

There may be more, but those are all I can think of at the moment (and the third is questionable -- it's really the expression that's the xvalue, but with something like return x; (where x is a local variable and you're returning the value, not a reference), the name of the variable is the expression. The name really refers to a glvalue, and in the expression that value (but not really the name) gets converted to an xvalue (which is an rvalue).