C++ 11(2.0)新特性 09(RValue Reference ROV技术)


C++2.0 右值引用

void print(char* str)
{
     
	cout << str << endl;
}

static size_t U_CTOR;
static size_t U_COPY_CTOR;
static size_t U_MOVE_CTOR;

class ValueReference
{
     
public:
	ValueReference() {
      }
	ValueReference(int len, string data) : len(len), data(data) {
      /*print("user ctor");*/ U_CTOR++; }
	ValueReference(const ValueReference & v) : len(v.len), data(v.data) {
      /*print("copy ctor");*/ U_COPY_CTOR++; }
	ValueReference(const ValueReference && v) : len(v.len), data(v.data){
      /*print("move ctor");*/ U_MOVE_CTOR++; }
	ValueReference & operator = (const ValueReference & v)
	{
     
		//print("copy operator=");
		return *this;
	}
	ValueReference & operator = (const ValueReference && v)
	{
     
		//print("move operator=");
		return *this;
	}

private:
	int len;
	string data;
};

void Outsize()
{
     
	cout << "U_CTOR:" << U_CTOR << endl;
	cout << "U_COPY_CTOR:" << U_COPY_CTOR << endl;
	cout << "U_MOVE_CTOR:" << U_MOVE_CTOR << endl;
	U_CTOR = 0;
	U_COPY_CTOR = 0;
	U_MOVE_CTOR = 0;
}

int main(int argc, char* argv[])
{
     
	// ---------- RV 基本概念 -------------
	// 左值:在'='左边的数据是左值,左值一般是用户声明的数据
	// 右值:在'='右边的数据是右值,右值一般是一个临时数据
	// 右值引用(ROV return of value):在传统的C++中有一个缺陷是每次返回的数据都要重新进行构造
	// C++2.0为了解决这个问题,引入了右值的概念
	int x = 3, y = 4;
	//x + y = 10; Err编译无法通过

	// ---------- RV 特殊情况 -------------
	string s1 = {
      "Cat" }, s2 = {
      "Dog" };
	s1 + s2 = "NiHao"; // 特殊的一些原因,自身的api库有些对规则不起作用
	string() = "Fish"; // 可以对临时对象赋值
	cout << "s1:" << s1 << " s2:" << s2 << "\n";

	complex<int> c1 = {
      1, 3 }, c2 = {
     3, 7};
	c1 + c2 = complex<int> {
      11, 13};
	complex<int>() = complex<int>{
      5, 3 };
	cout << "c1:" << c1 << " c2:" << c2 << "\n";

	// ---------- RV 实例 -------------
	// 调用端需要明确这个值是一个RValue,要不系统默认当copy来处理
	// 被调用端有声明如果是一个RValue我要怎么处理
	ValueReference v1 = {
      0, "data" };
	ValueReference v2 = {
      v1 }; //       v1调用端 v2被调用端 默认copy ctor
	// move方法可以明确一个对象使用move ctor
	ValueReference v3 = {
      move(v1) };//v1明确告诉编译器,我要调用move ctor

	// ---------- RV copy 和 move区别 -------------
	// copy 和 move的区别
	// copy是实例化一个新的数据,复制并且保留原来的数据
	// move是直接引用原来的数据,克隆不确定原来的数据存留
	// C++2.0之后在STL中的容器都有添加对应的右值引用的方法
	// vector list deque multiset unordered_multiset :iterator insert(const_iterator _Where, _Ty&& _Val)

	// ---------- RV 对STL容器的测试 TODO ------------
	vector<ValueReference> vvc = {
     }, vvr = {
     };
	list<ValueReference> ll = {
     };
	deque<ValueReference> dd = {
     };
	multiset<ValueReference> mm = {
     };
	// unordered_multiset uu = {};

	// 测试vector
	// vector是一维的数据存储结构
	long start_c = clock();
	for (int i = 0; i < 100000; ++i)
	{
     
		ValueReference v = {
      i, to_string(i) };
		vvc.push_back(v);
	}
	long copy_c = clock() - start_c;
	cout << "copy time:" << copy_c << endl;
	Outsize();
	for (int i = 0; i < 100000; ++i)
	{
     
		vvr.push_back({
      i, to_string(i) });
	}
	long move_c = clock() - start_c - copy_c;
	cout << "move time:" << move_c << endl;
	Outsize();
}

你可能感兴趣的:(Game,c++)