C++98

Historically, we have divided values into two categories, lvalue and rvalue.

An rvalue is a value that can only be on the right side of the equals sign, like a literal.

The lvalue, the value that appears on the left-hand side of the equals sign, can also appear on the right-hand side of the equals sign.

Such as

a = 1; // a is an lvalue and 1 is an rvalue. // This 1 is called a literal

A const int can be classified as neither an lvalue nor an rvalue. (Can appear on the left side of the equals sign only when initialized)

So in C, an lvalue represents the value of an object, such as a variable, a pointer, and so on. In C++98, we also changed functions to lvalues.

The nice thing about an lvalue is that you can bind an upper lvalue reference. If an rvalue is referenced, the reference must be a constant reference.

int a = 100; // a is an lvalue int &b = a; // b is an lvalue reference, so anything done to b is done to A

C++11

A new semantics, mobile semantics, was introduced in C++11. Specifically, you can move constructs, and you can move assignments.

Mobile semantics are a bit like “recycling”. If you use the move construct, you can move data from your body to new members, avoiding unnecessary data replication. For example, to move thousands of STD ::string members, in C++98 you can only copy and delete one, but in C++11 you can easily change the position of STD ::string internal Pointers.

Note, however, that only waste can be used. We’ll give this type of “waste” a name called Xvalue. The X stands for “expiring”. The original rvalue is subdivided into “pure rvalue” PRValue (pure rvalue)

So in C++11, there are three data types:

lvalue

xvalue

prvalue

Xvalue and PRValue are collectively rvalue. Lvalue and xvalue are collectively glValue.

Let’s do some examples.

prvalue:

Literals (except strings)

Built-in post-increment expressions like a++ (return a temporary object)

Built-in operations such as a+b, logical operations, etc

The return value of a function that returns a non-reference type

Cast to a non-reference type

Lambda expressions

And so on. For more specific categories, see extended reading.

xvalue:

The return value of a function that returns an rvalue reference. Such as STD: : move (x)

An rvalue reference was cast

That is, by using STD :: Move (x) to change the type of x to xvalue, the move constructor can be called (if implemented).

(Tip: Do not write return STD ::move(x); For this statement, return x; Ok, otherwise it will interfere with compiler optimization. See here)

Further, we can abstract these two judgment rules:

Rule 1: Can you tell if two expressions refer to the same object? For example, we can compare addresses.

Rule 2: Can you use mobile semantics? For example, see if you can use the call move constructor.

That’s xvalue

If it satisfies 1 and not 2, it’s lvalue

If it satisfies 2 but not 1, it’s prvalue

If it meets 1, it is called GLValue, and if it meets 2, it is called Rvalue

Having so many classes comes into play when it comes to binding references. For example, with different function overloads, an Xvalue will find an rvalue reference first, and a constant lvalue reference second, so that moving semantics can be properly implemented.

C++17

The classification is the same as C++11, but more semantically explicit.

Glvalues: long-lived object with its own address

Prvalues: Short-lived objects used for initialization

Xvalue: A long-lived object that the resource is no longer needed and can be reused

C++17 also introduced some new syntax provisions, and sometimes a prvalue can be materialized into an xvalue. Of course, don’t worry too much about these, just write the question.

In learning, we often encounter some problems, especially the knowledge don’t understand, my advice is to write down, jump in the past, because the connection between the knowledge of programming is very tight, but also is loose, a question that the road is blocked, we can change train of thought to solve, there is something that the present is not clear, waiting for you to fix up the back of the knowledge, It’s very common to look back and think “that’s the way it is”, so don’t settle for one place.

Finally, when you encounter problems, it is also very important to ask big gods. It is suggested to chat with this group and discuss with seniors. You will also get a lot of help. Can also exchange learning experience, technical problems, you can get PDF books source code, tutorials and so on for free use.