C++11中std::move的使用

std::move is used to indicate that an object t may be "moved from", i.e. allowing the efficient transfer of resources from t to another object. In particular,std::move produces an xvalue expression that identifies its argument t. It is exactly equivalent to a static_cast to an rvalue reference type.

In C++11, in addition to copy constructors, objects can have move constructors. (And in addition to copy assignment operators, they have move assignment operators.) The move constructor is used instead of the copy constructor, if the object has type "rvalue-reference" (Type &&). std::move() is a cast that produces an rvalue-reference to an object, to enable moving from it.

It's a new C++ way to avoid copies. For example, using a move constructor, a std::vector could just copy its internal pointer to data to the new object, leaving the moved object in an incorrect state, avoiding to copy all data.

在C++11中,标准库在<utility>中提供了一个有用的函数std::move,std::move并不能移动任何东西,它唯一的功能是将一个左值强制转化为右值引用,继而可以通过右值引用使用该值,以用于移动语义。从实现上讲,std::move基本等同于一个类型转换:static_cast<T&&>(lvalue);

std::move函数可以以非常简单的方式将左值引用转换为右值引用。

通过std::move,可以避免不必要的拷贝操作。

std::move是为性能而生。

std::move是将对象的状态或者所有权从一个对象转移到另一个对象,只是转移,没有内存的搬迁或者内存拷贝。

下面是从其他文章中copy的测试代码,详细内容介绍可以参考对应的reference:

#include "move.hpp"
#include <iostream>
#include <utility>
#include <vector>
#include <string>

// Blog: http://blog.csdn.net/fengbingchun/article/details/52558914

//
// reference: http://en.cppreference.com/w/cpp/utility/move
int test_move1()
{
	std::string str = "Hello";
	std::vector<std::string> v;

	// uses the push_back(const T&) overload, which means we'll incur the cost of copying str
	v.push_back(str);
	std::cout << "After copy, str is \"" << str << "\"\n";

	// uses the rvalue reference push_back(T&&) overload, which means no strings will be copied;
	// instead, the contents of str will be moved into the vector.
	// This is less expensive, but also means str might now be empty.
	v.push_back(std::move(str));
	std::cout << "After move, str is \"" << str << "\"\n";

	std::cout << "The contents of the vector are \"" << v[0] << "\", \"" << v[1] << "\"\n";

	return 0;
}


// reference: http://www.ibm.com/developerworks/cn/aix/library/1307_lisl_c11/
void ProcessValue(int& i)
{
	std::cout << "LValue processed: " << i << std::endl;
}

void ProcessValue(int&& i)
{
	std::cout << "RValue processed: " << i << std::endl;
}

int test_move2()
{
	int a = 0;
	ProcessValue(a);
	// std::move函数可以以非常简单的方式将左值引用转换为右值引用
	ProcessValue(std::move(a));

	return 0;
}

/
// reference: http://www.cplusplus.com/reference/utility/move/
int test_move3()
{
	std::string foo = "foo-string";
	std::string bar = "bar-string";
	std::vector<std::string> myvector;

	// The first call to myvector.push_back copies the value of foo into
	// the vector (foo keeps the value it had before the call).
	// The second call moves the value of bar into the vector.
	// This transfers its content into the vector(while bar loses its value,
	// and now is in a valid but unspecified state)
	myvector.push_back(foo);                    // copies
	myvector.push_back(std::move(bar));         // moves

	std::cout << "myvector contains:";
	for (std::string& x : myvector)
		std::cout << ' ' << x;
	std::cout << '\n';

	return 0;
}

/
int test_move4()
{
	std::string str1{ "abc" }, str2;
	fprintf(stdout, "str1: %s\n", str1.c_str()); // abc
	std::move(str1);
	fprintf(stdout, "str1: %s\n", str1.c_str()); // abc
	str2 = std::move(str1);
	fprintf(stdout, "str1: %s\n", str1.c_str()); // 
	fprintf(stdout, "str2: %s\n", str2.c_str()); // abc

	return 0;
}
GitHubhttps://github.com/fengbingchun/Messy_Test

  • 4
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值