13

@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?

Community
  • 1
  • 1
Xeo
  • 129,499
  • 52
  • 291
  • 397
  • 4
    Technically, `this`, `true`, and `false` are not names. They're tokens which form valid rvalue expressions and which happen to look like identifiers. – aschepler May 28 '11 at 21:25

2 Answers2

9

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).

Johannes Schaub - litb
  • 496,577
  • 130
  • 894
  • 1,212
8
  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).

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
  • The third point only applies to local variables returned by value. And even then the expression is still an lvalue, but there is a special language rule that treats the object *as if* it were designated by an rvalue. – fredoverflow May 28 '11 at 15:20
  • @FredOverflow: yes -- I edited in the mention of it having to be local for any of this to apply (and as I said, I agree that the third is questionable anyway). – Jerry Coffin May 28 '11 at 16:07