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

How to refer to the right value of C++

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

Share

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

This article introduces the relevant knowledge of "how to quote C++ right value". In the operation process of actual cases, many people will encounter such difficulties. Next, let Xiaobian lead you to learn how to deal with these situations! I hope you can read carefully and learn something!

1. L-value and R-value

In our previous articles, we introduced l-value references. C++11 introduces r-value references on the basis of l-value references, which are new features and are not used frequently, so there is a certain learning cost.

Let's put aside the concept of reference and first look at what is an l-value and what is an r-value. In fact, it is very simple, the left value can take the address, located on the left side of the equal sign. And the right value cannot take the address, located on the right side of the equal sign.

int a = 4;

For example, we define a variable a of type int and set its value equal to 4. where a is to the left of the equals sign, and we can find the address of a. And 4 is to the right of the equals sign, so we have no way to address 4. So a is left and 4 is right.

Another example:

int test() { return 4;}int a = test();

Similarly, a is to the left of the equals sign, and there is a way to take the address as an L-value. And test() is a temporary value that can't take an address, it's a right value.

So here it is relatively clear that variables with addresses are left values, constant values without addresses, temporary variables are right values.

2. L-value references and r-value references

Understand the concept of l-value, r-value and then look at the l-value reference, r-value reference is simple. An l-value reference is, by definition, a reference that can point to an l-value but not an r-value.

int a = 4;int &b = a; //legal int &c = 4; //illegal

However, there are exceptions to l-value references, that is, l-value references modified with const can point to r-values:

const int &b = 4;

Because const-modified references cannot be changed, they can point to rvalues. If you go through STL code, you will find that some of these functions have const & input parameters in order to be compatible with constant arguments. For example, push_back in vector:

void push_back (const value_type& val);

An r-value reference is similar to an l-value reference in that it can point to an r-value but not an l-value. To distinguish it from an l-value reference, use &&, i.e. two ampersand. To be honest, this symbol is confusing because it has the same meaning as and.

int a = 4;int &&b = 4; //legal int &&c = a; //illegal

The reason the third line above is illegal is that c is an rvalue reference and cannot point to an lvalue. What if we have to point? There is also a way to use the std::move function, which converts an lvalue to an rvalue.

using namespace std;int a = 4;int &&c = move(a);

The move function sounds like a move, but it doesn't actually move the variable, it just does something similar to a type conversion.

I don't know if you feel big when you see this. In fact, it's not over yet. There's one more important point. That is, both l-value references and r-value references are themselves l-value references:

void test(int && tmp) { tmp = 2333;}using namespace std; int a = 4; int &b = a; int &&c = 4; test(a);//illegal test(b);//illegal test(c);//illegal test(move(a));//legal test(move(b));//legal test(move(c));//legal

References in C++ are a very large category, and there are many details besides l-value references and r-value references. For example, universal reference, reference folding, perfect forwarding, etc.

"C++ right value how to quote" the content is introduced here, thank you for reading. If you want to know more about industry-related knowledge, you can pay attention to the website. Xiaobian will output more high-quality practical articles for everyone!

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