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

C++ right reference, Mobile semantics and perfect forwarding method

2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly explains "C++ right value reference, mobile semantics and perfect forwarding method", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Now let the editor to take you to learn "C++ right value reference, mobile semantics and perfect forwarding method"!

Clockwise music-left and right values, right value references, mobile semantics and perfect forwarding

In C++ or C language, an expression (which can be a literal quantity, a variable, an object, the return value of a function, etc.) can be divided into left-value expression and right-value expression according to its usage scene.

I. definition of left and right values

1. The left value is locator value in English and abbreviated to lvalue, which means data stored in memory with a clear storage address (addressable)

two。 The right value is read value in English and abbreviated to rvalue, which refers to data that can provide data values (not necessarily addressable, such as data stored and registers).

Second, how to determine whether an expression is left or right (in most scenarios)

1. The expression to the left of the assignment sign (=) is the left value; conversely, the expression that can only be located to the right of the assignment sign is the right value. For example:

Int a = 5

Where the variable an is a left value and the literal quantity 5 is a right value.

Note: the left value in C++ can be used as the right value, and vice versa, such as

Int b = 10; / / b is a left value a = b; / / a, b are all left values, but b can be used as a right value 10 = b; / / error, 10 is a right value and cannot be used as a left value

two。 The expression with a name that can get the storage address is the left value; otherwise, it is the right value.

Take the variables an and b defined above as an example, if an and b are variable names, their storage addresses can be obtained through & an and & b, so both an and b are left values; conversely, the literals 5 and 10 have neither a name nor their storage address (literal quantities are usually stored in registers or with code), so 5 and 10 are right values.

III. Right value reference of C++

There is a reference in the C++ 98apt 03 standard, but the reference here can only operate on the left value and cannot add a reference to the right value, so it is called a left value reference, for example:

Int num = 10int int & b = num; / / correct int & c = 10; / / error

Note that although the C++ 98amp 03 standard does not support non-constant left-value references for right values, it allows the right value to be manipulated using constant left-value references. That is, the constant left reference can manipulate either the left value or the right value, for example:

Int num = 10 Const int & b = num; / / correct const int & c = 10; / / correct

Why do I need a right value reference?

The right value often has no name, so it can only be used by reference. This gives rise to a problem that we may need to modify the right value in actual development (such as when implementing mobile semantics), but it is impossible to do so by using the constant left value reference.

To this end, the new standard Category 11 introduces another way of reference, which is called a right value reference, represented by & &.

It is important to note that, like declaring the left value reference, the right value reference must be initialized immediately and can only be initialized with the right value, such as:

Int num = 10int int & & a = num; / / error, right value reference cannot be initialized to left value & & a = 10; / / correct

Unlike the constant left value reference, the right value reference can also modify the right value. For example:

Int & & a = 10; a = 100; / / correct

In addition, C++ syntactically supports defining references to the right value of constants, for example:

Const int&& a = 10

However, this defined right value reference is of no practical use. On the one hand, the right value reference is mainly used for mobile semantics and perfect forwarding, in which the former needs the permission to modify the right value; secondly, the function of the constant right value reference is to reference an immutable right value, and the work constant left value reference can be completed.

IV. Std::move () and mobile semantics

With the help of the right-value reference, a mobile constructor can be added to a specified class, so that when a similar object is initialized with the right-value object of the class (which can be understood as a temporary object), the compiler will give priority to the mobile constructor.

Note: the timing of the mobile constructor is to initialize the new object with a similar right-value object. So how do I call the mobile constructor when initializing a similar object with a left-value object of this class (an instance object with a name that can get its storage address)? The solution given by Clover 11 is to call the std::move () function.

Although move means to move, this function does not move any data. Its function is to force a left value to a right value, which is often used to implement mobile semantics.

Usage example:

# include#includeusing namespace std;class movedemo {public: movedemo (): num (new int (0)) {cout

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