Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of value category and move semantics in Clipper 11

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly shows you the "sample analysis of value category and move semantics in Category 11", which is easy to understand and well-organized. I hope it can help you solve your doubts. Let me lead you to study and learn the article "sample Analysis of value category and move semantics in Craft 11".

Preface

There are only two types of value categories before Category 11, lvalue and rvalue. After Centrum 11, there are new value categories, namely prvalue, glvalue, and xvalue. Not understanding value categories may make us wonder how to fix it when we encounter some pitfalls, so understanding value categories is more important for C++ writers. The understanding of value categories is inseparable from a concept-move semantics. I'm sure anyone who knows about move semantics 11 knows the concepts of std::move, right-value reference, mobile construction / mobile copy, etc., but there may be many people who are vague about the exact definition of the concept.

What is move semantics (Mobile semantics)?

Semantics is a concept from linguistics, and its translation into Chinese means "semantics". When it comes to computer language, many people may think that it is a subcategory of computer science. In fact, he is a cross-subject of computer science and linguistics, in which many concepts come from linguistics, and even some students in the language class go to do compilation research / work later. So let's start with natural language and get a better understanding of move semantics through analogy. Here are two sentences:

He's a git.

This is a git.

There is the word "git" in both sentences, but the meaning of "git" in the two sentences is different. From a grammatical point of view, these two are the forms of "git". Only the pronouns are different, but the meaning of the sentence is completely different. Sentence 1 means that it is useless to scold a person, and sentence 2 means that this object is a bucket of rice. This example shows that to understand the meaning of a word (for example, "git"), it is necessary to combine other words in the sentence, as well as the whole sentence.

It is similar in the C++ language. There are two "sentences" below:

Vec = vector ()

Vec = another_vec

Where vec and another_vec are variables of type vector.

Both statements are in the form of "vec = XXXX;", but statement 1 moves XXXX to the variable vec, and statement 2 copies XXXX to vec. There is an "=" operator in both statements, but statement 1 means "move to" and statement 2 means "copy to". So the meaning of the "=" operator and the entire sentence is determined by the type of XXXX. We can say that statement 1 has the meaning of moving, statement 2 has the meaning of copy, or the "=" in statement 1 means moving, and the "=" in statement 2 means copy. More formally, statement 1 presents mobile semantics, statement 2 presents copy semantics, the "=" in statement 1 presents mobile semantics, and the "=" in statement 2 presents copy semantics. In English, it is statement 1 displayed move semantics; statement 2 displayed copy semantics; operator= in statement 1 displayed move semantics; operator= in statement 2 displayed copy semantics.

In fact, the translation of "mobile semantics" into vernacular means "mobile".

How to understand 5 kinds of value categories (value categories)?

Every expression in C++ has two attributes, one is type (type), and the other is value category (value category). The value category of each expression must belong to and only belong to one of the three categories: prvalue (pure rvalue), xvalue, and lvalue. Prvalue and xvalue are collectively referred to as rvalue,xvalue and lvalue are collectively referred to as glvalue (generalized lvalue), as shown in the following figure:

So, how are prvalue,xvalue and lvalue defined?

Actually, all expressions have the following two attributes:

Whether there is an identity (identity, or "identity"): whether it can be compared with another expression or object to determine whether it is the same entity. For example, if you have an address, you can compare them to the same address.

Whether it can be moved: if it appears in a statement such as assignment, initialization, etc., whether it will make the statement present mobile semantics.

So there are:

There is identity, and the expression that can be moved is xvalue expression.

An expression that has identity but cannot be moved is an lvalue expression

There is no identity, but the expression that can be moved is a prvalue expression

As for expressions without identity and cannot be moved, there is no such expression in practical application, and there is no need for such an expression.

For the other two value categories, we can summarize it as follows:

An expression with identity, and the value category is glvalue

An expression that can be moved, with a value category of rvalue.

Analyze and understand the rules that determine value categories in C++ standards

The C++ standard provides a series of rules that specify which expressions have which value categories. We can understand these rules in combination with the value category definitions given above. For example, for xvalue expressions, there are rules like this:

If an expression is a function call or overloaded operator expression and its return type is a right-value reference, such as std::move (x), then the expression is an xvalue expression

For this rule, we can understand it like this: first of all, if you want to return an object, you must reserve memory space on the stack, so the object has identity. Second, the return type is a right-value reference, so it makes statements that use this expression render mobile semantics, so they are mobile. Therefore, this expression is an xvalue expression.

There are such rules for xvalue.

Object member expression, that is, "a.m", which is a xvalue expression if an is a right value and m is a non-static data member of a non-reference type

This rule can be understood like this: first of all, an is the right value, that is, it can be moved, so m should also be movable as part of the an object. Second, access the object's "." The operator actually calculates the address offset, and since there is an address, there must be an identity. Therefore, this expression is an xvalue expression.

Another example is:

An object member expression, that is, "a.m", which is a prvalue expression if m is a member enumerator or a non-static member function

The enumerator is actually a number when compiled; the member function actually points to the address of the code snippet, which is actually a number. Both numbers are determined at compile time. When cpu uses these numbers, they are placed directly inside the instruction or in registers, not in memory, so they do not have identity. In fact, from another point of view, because they are only a value, not a variable, it is reasonable not to have identity. Therefore, this expression is an prvalue expression.

The C++ standard also defines many such rules that can be analyzed and understood in a similar way without having to memorize them.

The above is all the contents of the article "sample Analysis of value category and move semantics in Craft 11". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report