左右值、移动语义、完美转发

https://www.bilibili.com/video/BV1Df4y1C7xs?p=2
https://www.bilibili.com/video/BV1aE411c7eY/?spm_id_from=333.788.recommend_more_video.0
https://www.bilibili.com/video/BV1DB4y1P7ne?from=search&seid=7695802651750305366

https://segmentfault.com/a/1190000016041544
对于一个左值,肯定是调用拷贝函数。
但是有些左值是局部变量,什么周期也很短,这个也可以利用下这个局部的、生命周期的左值。
c++使用std:move()方法来将左值转换为右值。这样就可以使用移动构造了。

#include <iostream>
#include <cstring>
#include <vector>
using namespace std;

class MyString
{
public:
	static size_t CCtor; //统计调用拷贝构造函数的次数
	static size_t MCtor; //统计调用移动构造函数的次数
	static size_t CAsgn; //统计调用拷贝赋值函数的次数
	static size_t MAsgn; //统计调用移动赋值函数的次数

public:
	// 构造函数
	MyString(const char* cstr = 0) {
		if (cstr) {
			m_data = new char[strlen(cstr) + 1];
			strcpy(m_data, cstr);
		}
		else {
			m_data = new char[1];
			*m_data = '\0';
		}
	}

	// 拷贝构造函数
	MyString(const MyString& str) {
		CCtor++;
		m_data = new char[strlen(str.m_data) + 1];
		strcpy(m_data, str.m_data);
	}
	// 移动构造函数
	MyString(MyString&& str) noexcept
		:m_data(str.m_data) {
		MCtor++;
		str.m_data = nullptr; //不再指向之前的资源了
	}

	// 拷贝赋值函数 =号重载
	MyString& operator=(const MyString& str) {
		CAsgn++;
		if (this == &str) // 避免自我赋值!!
			return *this;

		delete[] m_data;
		m_data = new char[strlen(str.m_data) + 1];
		strcpy(m_data, str.m_data);
		return *this;
	}

	// 移动赋值函数 =号重载
	MyString& operator=(MyString&& str) noexcept {
		MAsgn++;
		if (this == &str) // 避免自我赋值!!
			return *this;

		delete[] m_data;
		m_data = str.m_data;
		str.m_data = nullptr; //不再指向之前的资源了
		return *this;
	}

	~MyString() {
		delete[] m_data;
	}

	char* get_c_str() const { return m_data; }
private:
	char* m_data;
};
size_t MyString::CCtor = 0;
size_t MyString::MCtor = 0;
size_t MyString::CAsgn = 0;
size_t MyString::MAsgn = 0;
int main()
{
	vector<MyString> vecStr;
	vecStr.reserve(1000); //先分配好1000个空间
	for (int i = 0; i < 1000; i++) {
		MyString temp = MyString("hello");
		vecStr.push_back(std::move(temp)); //使用move将左值temp转为右值使用。
	}
	cout << "CCtor = " << MyString::CCtor << endl;
	cout << "MCtor = " << MyString::MCtor << endl;
	cout << "CAsgn = " << MyString::CAsgn << endl;
	cout << "MAsgn = " << MyString::MAsgn << endl;
}

/* 结果
CCtor = 0
MCtor = 1000
CAsgn = 0
MAsgn = 0
*/

#include <iostream>
#include <type_traits>
#include <string>
using namespace std;

template<typename T>
void f(T&& param) {
	if (std::is_same<string, T>::value)
	{
		std::cout << "string" << std::endl;
	}
	else if (std::is_same<string&, T>::value)
	{
		std::cout << "string&" << std::endl;
	}
	else if (std::is_same<string&&, T>::value)
	{
		std::cout << "string&&" << std::endl;
	}
	else if (std::is_same<int, T>::value)
	{
		std::cout << "int" << std::endl;
	}
	else if (std::is_same<int&, T>::value)
	{
		std::cout << "int&" << std::endl;
	}
	else if (std::is_same<int&&, T>::value)
	{
		std::cout << "int&&" << std::endl;
	}
	else
	{
		std::cout << "unkown" << std::endl;
	}
}

int main()
{
	int x = 1;
	f(1); // 参数是右值 T推导成了int, 所以是int&& param, 右值引用
	f(x); // 参数是左值 T推导成了int&, 所以是int&&& param, 折叠成 int&,左值引用
	int && a = 2;
	f(a); //虽然a是右值引用,但它还是一个左值, T推导成了int&
	string str = "hello";
	f(str); //参数是左值 T推导成了string&
	f(string("hello")); //参数是右值, T推导成了string
	f(std::move(str));//参数是右值, T推导成了string
	exit(0);
	return;
}

#include <iostream>
#include <cstring>
#include <vector>
using namespace std;

void RunCode(int &&m) {
	cout << "rvalue ref" << endl;
}
void RunCode(int &m) {
	cout << "lvalue ref" << endl;
}
void RunCode(const int &&m) {
	cout << "const rvalue ref" << endl;
}
void RunCode(const int &m) {
	cout << "const lvalue ref" << endl;
}

// 这里利用了universal references,如果写T&,就不支持传入右值,而写T&&,既能支持左值,又能支持右值
template<typename T>
void perfectForward(T && t) {
	RunCode(forward<T>(t));
}

template<typename T>
void notPerfectForward(T && t) {
	RunCode(t);
}

int main()
{
	int a = 0;
	int b = 0;
	const int c = 0;
	const int d = 0;

	notPerfectForward(a); // lvalue ref
	notPerfectForward(move(b)); // lvalue ref
	notPerfectForward(c); // const lvalue ref
	notPerfectForward(move(d)); // const lvalue ref

	cout << endl;
	perfectForward(a); // lvalue ref
	perfectForward(move(b)); // rvalue ref
	perfectForward(c); // const lvalue ref
	perfectForward(move(d)); // const rvalue ref
}

#include <iostream>
#include <cstring>
#include <vector>
using namespace std;


template<typename T>
void f(T&& t)
{
	if (std::is_same<int, T>::value) //T&&
	{
		cout << "int" << endl;
	}
	if (std::is_same<int&, T>::value) //T&&
	{
		cout << "int&" << endl;

	}
	if (std::is_same<int&&, T>::value) //T&&
	{
		cout << "int&&" << endl;
	}
}


int main()
{
	int a = 1;
	int& b = a;
	int&& c = 1;

	f(a);
	f(2);
	f(b);
	f(c);

	cout << endl;

	f(move(a));
	f(move(2));
	f(move(b));
	f(move(c));

	cin.get();
	return 0;
}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值