[C ++] - C ++ 11 white perspective of rvalue references

Rvalue references

In Benpian you will see:

  • What is the value left? What is the right value?
  • What is left value references? What is the right value references?
  • What is a mobile structure?

1, left and right values

Left and right values are the concept of the C language, C language but did not give a strict distinction, the general thought: can be placed on the left side of the equal sign or can take the address of the called lvalue, only on the right side of the equal sign or can not take the address as the right value. But not necessarily entirely correct.

Let's first look at a piece of code:

int main()
{
	int a = 10;
	int b = 20;

	//a和b都是左值,b既可以在=的左侧,也可以在右侧,如下:
	a = b;
	b = a;
	//说明:左值既可以放在=的左侧,也可以放在=的右侧

	const int c = 30;
	c = a;  //编译报错,c为const 常量,只允许读不允许修改
	
	 //因为可以对c取地址,因此c严格的来说不算是左值,如下:
	cout << &c << endl;

	//编译失败,因为b+1的结果是一个临时变量,没有具体名称,不能取地址,因此为右值,如下:
	b + 1 = 20;

	
	system("pause");
	return 0;
}

See here, if you have a little sense of left and right values? In fact, can be understood: For an expression, those who take their address (&) operation can be successful are left value, otherwise, it is the right value

Note that: temporary object is the right value

to sum up:

1, the general type of variable, because of the name, you can take the address, all that is left value
2, const modified constants can not be modified, system types, theory in accordance with the right values to treat both, but because it can take the address, C + +11 believe it is the left value
3, if you run a temporary result of the expression is an object or an object, believed to be the right value
4, operating results or if the expression is a single variable reference value is considered to be left

2, left and right reference value reference value

Knowing the left and right values, let us talk about left and right reference value reference value. We know that C ++ 11 is the right value can not be modified, but rvalue references appeared to break this pattern, it allows us to get the right reference value and modify it.

A second look at the code to understand:

int main()
{
	int a = 10;
	//普通类型只能引用左值,不能引用右值,如下:
	int& ra1 = a;   //ral为a的别名
	//int& ra2 = 10;  //编译失败,因为10是右值

	//const引用既可以引用左值,也可以引用右值,如下:
	const int& ra3 = 10;
	const int& ra4 = a;
	system("pause");
	return 0;
}

Summarized in one sentence: ordinary type can only refer to the value of the left, not the right reference value, but can refer to either the left const reference value, it can also refer to the right value

Rvalue reference type is: && a = type of object is modified. as follows:

int main()
{
	//10本来是纯右值,本来是一个符号,没有具体空间
	//右值引用变量r1在定义过程中,编译器产生了一个临时变量,r1实际引用的是临时变量
	int &&r1 = 10;
	r1 = 100;

	int a = 10;
	//int&& r2 = a;  //编译失败,因为右值引用不能引用左值
	int&& r3=move(a);  //但是加上move之后可以引用左值
	return 0;
}

to sum up:

  • Lvalue references and rvalue references are to the object alias
  • Left value is usually: can be modified object
  • The right value is usually: constants, expressions return value
  • Left reference value can lvalue
  • const reference value can be left and right values
  • Right values ​​can reference the right value
  • Rvalue references can move to the left reference value

3, the limitations copy constructor

(Why the sudden mention the copy constructor, because the mobile structure can solve the problem of the copy constructor) Here I define a class Test, Test and then call the constructor and copy constructor to initialize Test object t in the main function inside.
Here Insert Picture Description
Explain, you can see, in this case the copy constructor is called twice, once outside call Test class constructor, returns to the Function time function, once the object is initialized Test time t.
In fact, the two temporary objects does not make sense, the end of construction will be destroyed, that is, a value will perish, were unnecessary copies in the copy constructor, but fortunately, in the above code, I just opened one hundred int the space, in case it is millions or even hundreds of millions, the efficiency will be lower. It seems some truth, then how should we do? Looking back

4, the mobile constructor

Mobile structure is thought that since you (temporary objects) has a value of the dead, then I direct that brought your memory, you will no longer need to slowly open space to copy your data to me, that is directly the above code we _a pointed directly to that memory take over, and then put before the 100 integer value to copy over. In fact, this is the idea of moving the constructor .

About Mobile constructor is a configuration function, but it accepts a parameter value of this class object reference to the right, for the above example, its movement constructor as follows:

Test(Test &&rhs):_a(rhs._a)
{
	rhs._a=nullptr;
}

In the initialization of the list of construction, we only had a shallow copy, _a (rhs._a), the object has been rhs application memory footprint, and at the same time rhs pointer assigned nullptr, which avoids the copy constructor memory problems caused by replication efficiency.

These are the information about the C ++ rvalue references, the author also scholarship soon, where there are problems, I hope the majority of users suggested that certain humbly accept

Published 33 original articles · won praise 13 · views 1038

Guess you like

Origin blog.csdn.net/Vicky_Cr/article/details/105107241