C++11 Value category (value Class) and move semantics (move semantics)

Source: Internet
Author: User

Reprint Please keep the following statement
Zhaozong
Source: http://www.cnblogs.com/zhao-zongsheng/p/value_categories_and_move_semantics.html

Before c++11 value categories there were only two classes, Lvalue and Rvalue, after c++11 there was a new value categories, namely Prvalue, Glvalue, Xvalue. Do not understand the value categories may let us encounter some pits do not know how to modify, so understand the value categories for C + + people are more important. and understanding value categories inseparable from a concept--move semantics. People who know c++11 I believe that we all know the concepts of std::move, rvalue Reference and mobile construction/mobile replication, but there are many people who are vague about the exact definition of the move semantics concept. I would like to use this article to talk about my understanding of value categories and move semantics. Start with move semantics first.

What is move semantics (moving semantics)?

Semantics is a concept from linguistics, which translates into Chinese as "semantics". Speaking of computer language, many people may think that he is a sub-category of computer science. In fact, he is a computer science and linguistics cross-subject, there are many concepts from the content of linguistics, but also the language of the students to do the compilation of research/work. So we start with natural language, and we can better understand move semantics by analogy. Here are two sentences:

    1. He's a loser.
    2. This is a git.

These two sentences have the word "git", but the meaning of "git" in two sentences is different. From the grammatical point of view, these two are "< pronoun > is the git" form, only the pronoun is different, but the sentence meaning is completely different. Sentence 1 means to scold a person is useless, sentence 2 means that the object is Sheng fan barrels. This example shows that to understand the meaning of a word (for example, "Git") is to combine the other words in the sentence, combined with the whole sentence, or even to combine the sentence to understand.

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

    1. VEC = vector<int> ();
    2. VEC = Another_vec;

Of these, VEC and Another_vec are variables of the vector<int> type.

Both of these statements are "VEC = XXXX;" form, but statement 1 is to move XXXX to the variable VEC, statement 2 is to the XXXX copy to VEC. There are "=" operators in all two statements, but in statement 1 the meaning is "move to" and the meaning in statement 2 is "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 a moving meaning, statement 2 has a copy of the meaning, or that the "=" in statement 1 is the meaning of movement, statement 2 "=" is the meaning of the copy. More formally, Statement 1 renders the move semantics, statement 2 renders the copy semantics, the "=" in statement 1 renders the move semantics, and the "=" in statement 2 renders the copy semantics. In English said 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, "mobile semantics" translated into the vernacular is "the meaning of movement."

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

Each expression in C + + has two properties, one is type, and the other is the value category. The value category of each expression must belong to and belong to only Prvalue (pure rvalue), Xvalue, one of the Lvalue three types. Prvalue and Xvalue are collectively referred to as Rvalue,xvalue and Lvalue collectively known as Glvalue (generalized lvalue), as shown in:

So, how do prvalue,xvalue and Lvalue define it?

In fact, all expressions have the following two properties:

    1. Whether there is an identity (identity, or "identity"): whether it can be compared with another expression or an object to determine whether it is the same entity. For example, if you have an address, you can compare their address to the same;
    2. Whether it can be moved: if it appears in an assignment, initialization, and so on, whether the statement renders the move semantics.

So there are:

    1. There is an identity, can also move the expression is xvalue;
    2. There is an identity, but the expression that cannot be moved is lvalue;
    3. There is no identity, but the expression that can be moved is prvalue;

As for expressions that do not have an identity and cannot be moved, there is no such expression in the actual application, nor is it necessary to have such an expression.

For the other two value categories, we can summarize this:

    1. There is an identity expression, the value category is Glvalue;
    2. The expression that can be moved, the value category is Rvalue.
Analyze rules that understand value categories in the C + + standard

For example, there are rules for Xvalue expressions:

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

For this rule, we can understand that. First returns an object, which must be reserved for memory space on the stack, so this object is by identity. The return type is an rvalue reference, so it is possible to move a statement that uses this expression to render the moving semantics. Therefore, this expression is an xvalue expression.

There are rules for xvalue.

An object member expression, "a.m.", if a it is an rvalue and a m non-static data member of the non-reference type, the expression is a xvalue expression

This rule can be understood, a is the right value, that is, can move, then for a part of a object, m should also be movable. Access to the object's "." The operator is actually the displacement operation of the pointer, and since the pointer is used, there must be an address. Therefore, this expression is an xvalue expression.

Another example:

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

Whether the static enumerator is actually a number, the member function actually refers to the address of the code snippet, which is actually a number, and is a number that is determined at compile time. When the CPU is operating on these numbers, these numbers are placed directly inside the instruction, or in registers, and not in memory, so they have no identity. So this expression is prvalue expression.

The C + + standard also defines a number of rules that specify which expressions are Prvalue, which are xvalue, and which are lvalue. These rules can be analyzed and understood in a similar way, without the need to memorize.

C++11 Value category (value Class) and move semantics (move semantics)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.