Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the meaning of "identity" in the definition of value categories in C++

In short, you can just answer the part about identity, thanks. My main focus of this question is start from 2. about identity, I just tried to provide context/background of my current understanding so it may help you decide the depth when you're writing your answer.


I want to understand the big picture of type system and value categories in C++. I've searched/read many questions and resources online, but everyone has a distinct explanation, so I'm really confused. I'll list the part I can't grasp, if anyone could provide idea about

  1. On cppreference.com, first line:

    Objects, references, functions including function template specializations, and expressions have a property called type, which both restricts the operations that are permitted for those entities and provides semantic meaning to the otherwise generic sequences of bits.

    my question:

    • What does it mean an expression has a type? Is it the type of the final result after evaluation?
    • I don't want to learn template at current stage, would this hinder learning about the basic part (from you expert's perspective)? It took me some time to realize that forwarding reference and rvalue reference are different thing, which the former is for template.
  2. Value categories:

    I read this answer of - What are rvalues, lvalues, xvalues, glvalues, and prvalues?, the phrase bother me a lot is identity, which also appears on cppreference.com - Value categories (Line 5, glvalue).

    my question: can I say that identity == everything I can assign a new value on it?

    • I saw people using the word address/pointer for it, but is that has identity iff has address/pointer? I want the precise term.
    • I came across the idea of bit-field when reading cppreference.com, it seems like given a bit-field struct a, its bit field a.m has no address? Is this the reason the word identity is used instead of address/pointer?
    • I found a blog post explaining this, but it's lvalue definition is counter-intuitive: an lvalue denotes an object whose resource cannot be reused, why not?
like image 725
Kindred Avatar asked Nov 23 '18 08:11

Kindred


People also ask

What is your definition of identity?

Identity is the unique set of characteristics that can be used to identify a person as themself and no one else. The word can be used in different ways in different contexts. On a personal level, identity often refers to a person's sense of self, meaning how they view themself as compared to other people.

What is identity example?

Examples of social identities are race/ethnicity, gender, social class/socioeconomic status, sexual orientation, (dis)abilities, and religion/religious beliefs.

Is identity fixed?

Identity is not: Fixed — identity is not something that remains the same throughout one's life. It changes over time. It can also change from situation to situation, over very short spans of time.

Where does the word identity come from?

identity (n.) c. 1600, "sameness, oneness, state of being the same," from French identité (14c.), from Medieval Latin identitatem (nominative identitas) "sameness," ultimately from Latin idem (neuter) "the same" (see idem). [For discussion of Latin formation, see entry in OED.]


1 Answers

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.

like image 81
Oliv Avatar answered Sep 22 '22 13:09

Oliv