0

I'm rather confused about determiming the value category of an expression. Could you please provide the basic steps that should be taken (what should be analysed) to determing the value category of an expression?

beginpluses
  • 497
  • 2
  • 9

1 Answers1

1

If you just want a quick and usually correct answer, consider these rules of thumb:

  • If it's a function or an already existing object, then it's a glvalue.
    • Most glvalues are lvalues.
    • xvalues are the things that can specifically be moved from: casts to an rvalue reference type, or a function call where the function return type is an rvalue reference type (especially std::move and sometimes std::forward).
  • If it's just a value or a way of creating a new object, then it's a prvalue.

But there are some cases where the category could still be unclear. And the above involves some simplifications (in particular, the rules for A.B and A ? B : C are more complicated).

The only really reliable way is to look for your answer in the Standard.

  1. Determine what sort of expression you have in terms of the grammar. A literal? An operator expression? A lambda? Etc.

  2. If the expression is an operator expression, figure out if overload resolution will select some overloaded operator function or a built-in candidate operator, as described in [over.match.oper], [over.oper], and [over.built].

  3. If the expression is actually a call to an overloaded operator function, the value category is determined from the return type of the operator function selected by overload resolution, as described in [expr.call]. In this case, ignore the description of built-in operator behavior for this purpose.

  4. Otherwise, find the section of [expr.prim] or [expr.compound] (see the table of contents) for the grammatical form of the expression. That section will state how the value category of the expression is determined. It will often be necessary to know the types and value categories of any subexpressions, so you may need to follow these rules recursively.

aschepler
  • 70,891
  • 9
  • 107
  • 161