What's the meaning of "identity" in the definition of value categories in C++
The identity
is a philosophical concept. It's a property of a thing that makes it unique. No two "things" can have the same identity.
A something that has an identity is an entity.
[basic.lval]:
A glvalue is an expression whose evaluation determines the identity of an object, bit-field, or function.
A name inside an expression can only designate one object. So a name inside an expression is an identity. It is defined as an lvalue (for example see expr.prim.id.unqual)
At a given address and at given time there can not be 2 objects of the same type (there can be object nested inside each other, ...). So dereferencing a pointer gives an lvalue.
A reference always designates an entity. So every function that returns a reference when called generates a glvalue.
...
An xvalue is a tag, that can only be generated by a cast (or a bound to a temporary materialization). It is a glvalue that denotes an object or bit-field whose resources can be reused basic.lval
The difference between an xvalue and an lvalue is used to produce efficient code. But xvalue as lvalue are glvalue: they bring the identity of an entity.
...
A prvalue is the result of an expression that is not associated to any object. That is the result of a call to a function that has a non reference return type or the result of some built-in operator calls. In c++ an expression is not an entity, so it has no identity.
prvalues may have a result object, which can be a temporary object. A temporary is an entity, it is materialized when needed (when one try to get a reference to it or when a prvalue is discarded).
The type of an expression is clearly defined in [expr.type]:
If an expression initially has the type “reference to T” ([dcl.ref], [dcl.init.ref]), the type is adjusted to T prior to any further analysis. The expression designates the object or function denoted by the reference, and the expression is an lvalue or an xvalue, depending on the expression. [ Note: Before the lifetime of the reference has started or after it has ended, the behavior is undefined (see [basic.life]). — end note ]
If a prvalue initially has the type “cv T”, where T is a cv-unqualified non-class, non-array type, the type of the expression is adjusted to T prior to any further analysis.
An expression cannot have reference type.
- The type of an expression is the type its result would have, if and when the expression were evaluated. An expression doesn't have to be evaluated, but all expressions have type. Type is a static property.
- There's no precise definition of identity, or an indication of which entities do or do not have identity. It's a muddy concept that is better left alone. Ignore it. Some people say that object identity is its address, but then this concept is useless. Why not just talk about its address then? And what about bit fields? They are objects without addresses, don't they have identity? Others say that lvalues have identity and rvalues don't, but then it's just as redundant.