C++11新特性【右值引用】

30 篇文章 1 订阅
本文详细解释了左值、右值和它们的引用形式在C++中的概念,探讨了左值引用和右值引用的使用场景,以及如何通过完美转发在模板中处理不同类型的引用。重点强调了右值引用在减少拷贝和资源管理上的优势。
摘要由CSDN通过智能技术生成

1. 什么是左值

左值不能根据字面意思来理解,不是说在左边的就是左值,例如:

int main()
{
	int a = 0;
	int b = a;
	return 0;
}

这里的int b = aa在右边,但a也是左值。

左值可以对它进行取地址赋值,可以出现在赋值=符号的左边

int main()
{
	//左值
	int a = 0;
	int b = a;
	int* ptr = new int(11);
	const int ca = 100;
	return 0;
}

2. 什么是右值

右值出现在赋值符号的=右边,不能出现在赋值符号的左边,不能对其进行取地址,例如:

image-20240312090818335

对于"hello world"这样的常量字符串,它本身是右值,但是:

const char *pch = "hello world";

这个也能对其进行取地址,本质上编译器做出了隐式类型转换,将字符串字面值转换成指向字符常量的指针,将该字符串的首元素地址给了pch

3. 左值引用

引用在语法上来说就是取别名,左值引用就是给左值取别名:

int main()
{
	int a = 10;
	int& b = a;	//左值引用
	return 0;
}

4. 左值引用使用场景

左值引用大多数用于做参数传参做返回值,采用左值引用的核心价值就是减少拷贝

左值引用缺陷:

但是左值引用有一个缺陷就是不能返回局部对象

string& func()
{
	string s; 
	cin >> s;

	//...

	return s;	//退出函数生命周期结束
}

这里就算是返回的引用,但是这个局部对象的生命周期到了,所以只能传值返回,这里就有2次拷贝,生成临时对象拷贝一次,赋值又拷贝一次。

class A
{
public:
	A() :s("")
	{
		cout << "A()" << endl;
	}
	A(const string& str) :s(str)
	{
		cout << "A(const string &str):s(str)" << endl;
	}
	A(const A& copy) :
		s(copy.s)
	{
		cout << "A(const A& copy)" << endl;
	}

	~A()
	{
		cout << "~A()" << endl;
	}
private:
	string s;
};

A func()
{
	A a;
	return a;
}

int main()
{
	A ta = func();
	cout << endl;

	A tb;
	tb = func();

	cout << endl;
	A tc(tb);
}

//	输出:
//	A()
//	A(const A& copy)
//	~A()
//	
//	A()
//	A()
//	A(const A& copy)
//	~A()
//	~A()
//	
//	A(const A& copy)
//	~A()
//	~A()
//	~A()

5. 右值引用

右值引用即给右值取别名,用&&表示:

int Add(int x, int y)
{
	return x + y;
}

int main()
{
	int&& a = 10;
	//int& b = Add(2, 3);	//error
	int&& b = Add(2, 3);
	return 0;
}

左值引用也可以引用,但是要加上const

int main()
{
    int x = 20;
    int y = 30;
    const int& a = 10;
    const int& ret = x + y;
    return 0;
}

右值引用需要加上move才可以引用左值

int main()
{
    int x = 100;
    int&& y = move(x);
    return 0;
}

6. 右值引用使用场景

右值引用的核心价值也是减少拷贝,弥补左值引用的缺陷(传值返回)

6.1 场景1

自定义类型深拷贝必须返回的场景:

namespace mystring
{
	class string
	{
	public:
		typedef char* iterator;
		iterator begin()
		{
			return _str;
		}

		iterator end()
		{
			return _str + _size;
		}

		string(const char* str = "")
			:_size(strlen(str))
			, _capacity(_size)
		{
			_str = new char[_capacity + 1];
			strcpy(_str, str);
		}

		void swap(string& s)
		{
			::swap(_str, s._str);
			::swap(_size, s._size);
			::swap(_capacity, s._capacity);
		}

		// 拷贝构造
		string(const string& s)
			:_str(nullptr)
		{
			cout << "string(const string& s) -- 深拷贝" << endl;

			string tmp(s._str);
			swap(tmp);
		}

		string(string&& s)
			:_str(nullptr)
		{
			cout << "string(string&& s) -- 移动拷贝" << endl;

			swap(s);
		}

		// 赋值重载
		string& operator=(const string& s)
		{
			cout << "string& operator=(string s) -- 深拷贝" << endl;
			string tmp(s);
			swap(tmp);
			return *this;
		}

		string& operator=(string&& s)
		{
			cout << "string& operator=(string && s) -- 移动拷贝" << endl;
			swap(s);

			return *this;
		}

		~string()
		{
			delete[] _str;
			_str = nullptr;
		}

		char& operator[](size_t pos)
		{
			assert(pos < _size);
			return _str[pos];
		}

		void reserve(size_t n)
		{
			if (n > _capacity)
			{
				char* tmp = new char[n + 1];
				strcpy(tmp, _str);
				delete[] _str;
				_str = tmp;

				_capacity = n;
			}
		}

		void push_back(char ch)
		{
			if (_size >= _capacity)
			{
				size_t newcapacity = _capacity == 0 ? 4 : _capacity * 2;
				reserve(newcapacity);
			}

			_str[_size] = ch;
			++_size;
			_str[_size] = '\0';
		}

		string& operator+=(char ch)
		{
			push_back(ch);
			return *this;
		}

		const char* c_str() const
		{
			return _str;
		}
	private:
		char* _str;
		size_t _size;
		size_t _capacity; // 不包含最后做标识的\0
	};
}

mystring::string func()
{
	mystring::string str("hello");
	return str;
}

int main()
{
	mystring::string ret1;
	ret1 = func();
	cout << endl;

	mystring::string ret2 = func();
	return 0;
}

image-20240312114340818

内置类型的右值叫做纯右值自定义类型的右值叫做将亡值

因为内置类型的生命周期只在表达式这一行,再往后走就析构了,例如mystring::string ret2 = func(),这个func()的生命周期就在这一行。

右值引用就是资源互换,将亡值的资源拿过来,然后把自己不用的内容给将亡值带走

编译器会进行优化:

  1. 连续的构造/拷贝构造合并;
  2. str强行识别成右值——将亡值(每个编译器都这样!)

如果说我们要拷贝的对象是一棵树,这样减少了拷贝的资源,极大提升了性能

6.2 场景2

容器的插入接口,插入对象是右值,可以利用移动构造转移资源,减少拷贝

int main()
{
	list<mystring::string> lt;
	mystring::string s1("hello list1");
	lt.push_back(s1);
	cout << endl;
	mystring::string s2("hello list2");
	lt.push_back(move(s2)); 
    lt.push_back("hello list3");	//插入对象是右值
}

所有的容器都实现了右值版本:

image-20240312125531560

7. 完美转发

在模板中&&不代表右值引用,而是万能引用,即既能接收左值又能接收右值

void Func(int& x)
{
	cout << "左值引用" << endl;
}
void Func(int&& x)
{
	cout << "右值引用" << endl;
}
void Func(const int& x)
{
	cout << "const 左值引用" << endl;
}
void Func(const int&& x)
{
	cout << "const 右值引用" << endl;
}

template<class T>
void PerfectForward(T&& t)
{
	Func(t);
}

int main()
{
	int a = 20;
	PerfectForward(a);	//左值
	PerfectForward(100);	//右值
	const int cb = 30;
	PerfectForward(cb);	//const左值
	PerfectForward(move(cb));	//const右值
	return 0;
}

这里运行发现,结果全部都被识别成了左值:

image-20240312154642863

这里是因为引用的底层,都是指针,例如:

int main()
{
    int a = 10;
    int& lr = a;
    int&& rr = move(a);
    cout << &lr << endl;
    cout << &rr << endl;
}

这里的lr是左值,而rr的属性也是左值,右值是不能够修改和取地址的,而这里的rr既能取地址也能修改。

这里有点绕,比如说hello,这个字符串是右值,而string&& s = hellos是它的右值引用,s会开一个空间把这个值存起来,s的属性还是属于左值的,因为它能够修改和取地址

右值引用变量的属性会被编译器识别成左值,不然在移动构造的场景下,就无法进行资源转移。

要想其保持原有属性:左值引用左值属性,右值引用右值属性

可以完美转发forward

void PerfectForward(T&& t)
{
	Func(forward<T>(t));
}

image-20240312161139807

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

加法器+

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值