c++11rvaluexvaluevalue-categoriesprvalue

What steps should I take to determine the value category of an expression?


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?


Solution

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

    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.