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>

//////////////////////////////////////////////////////
// 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;
}

GitHubhttps://github.com/fengbingchun/Messy_Test

C++11中std::move的使用的更多相关文章

  1. C++11中std::move、std::forward、左右值引用、移动构造函数的测试

    关于C++11新特性之std::move.std::forward.左右值引用网上资料已经很多了,我主要针对测试性能做一个测试,梳理一下这些逻辑,首先,左值比较熟悉,右值就是临时变量,意味着使用一次就 ...

  2. c++11 中的 move 与 forward

    [update: 关于左值右值的另一点总结,请参看这篇] 一. move 关于 lvaue 和 rvalue,在 c++11 以前存在一个有趣的现象:T&  指向 lvalue (左传引用), ...

  3. C++11中std::forward的使用 (转)

    std::forward argument: Returns an rvalue reference to arg if arg is not an lvalue reference; If arg ...

  4. C++11中std::forward的使用

    std::forward argument: Returns an rvalue reference to arg if arg is not an lvalue reference; If arg ...

  5. C++11中std::function的使用

    class template std::function is a general-purpose polymorphic function wrapper. Instances of std::fu ...

  6. C++11中std::unordered_map的使用

    unordered map is an associative container that contains key-value pairs with unique keys. Search, in ...

  7. C++11中std::bind的使用

    std::bind: Each argument may either be bound to a value or be a placeholder: (1).If bound to a value ...

  8. item 23: 理解std::move和std::forward

    本文翻译自<effective modern C++>,由于水平有限,故无法保证翻译完全正确,欢迎指出错误.谢谢! 博客已经迁移到这里啦 根据std::move和std::forward不 ...

  9. 对C++11中的`移动语义`与`右值引用`的介绍与讨论

    本文主要介绍了C++11中的移动语义与右值引用, 并且对其中的一些坑做了深入的讨论. 在正式介绍这部分内容之前, 我们先介绍一下rule of three/five原则, 与copy-and-swap ...

随机推荐

  1. 读REDIS数据结构

    一.DICT 主要有两个问题: 1.散列冲突,解决办法是拉链法 typedef struct dictEntry { void *key; union { void *val; uint64_t u6 ...

  2. 【洛谷5288】[HNOI2019] 多边形(二叉树模型)

    点此看题面 大致题意: 给你一个多边形,用若干不重合.不相交的线段将其划分为若干三角形区域,并定义旋转操作\((a,c)\)为选定\(4\)个点\(a,b,c,d\)满足\(a<b<c&l ...

  3. HDU 2647 拓扑排序

    题意:每个人的工资至少888,然后有m个条件,前者比后者要多.求最少工资. 分析: 最开始的开邻接矩阵的肯定超时,如果dfs,会出现由于刚开始不是从入度为0的点出发,后期修改不了.比较麻烦. 正确方式 ...

  4. 【转】Activity、Window、View的关系

    1.先看一个现象 1 2 3 4 5 6 7 8 9 10 11 public class MainActivity extends Activity {       @Override     pr ...

  5. 2018.11.11 Java的 三大框架:Struts+Hibernate+Spring

    ·定义:Java三大框架主要用来做WEN应用.Struts主要负责表示层的显示: Spring利用它的IOC和AOP来处理控制业务(负责对数据库的操作): Hibernate主要是数据持久化到数据库. ...

  6. 深入 Struts2 的配置 - 处理多个请求-处理请求结果-模型驱动-异常机制

    转:http://www.java3z.com/cwbwebhome/article/article2/2938.html?id=1631 本部分主要介绍struts.xml的常用配置. 1.1.   ...

  7. Lucas定理及应用

    额,前两天刚讲了数据结构,今天我来讲讲组合数学中的一种奇妙优化——Lucas 先看这样一个东西 没学过lucas的肯定会说:还不简单?处理逆元,边乘边膜呗 是,可以,但注意一下数据范围 你算这一次,你 ...

  8. POJ 1180 Batch Scheduling (dp,双端队列)

    #include <iostream> using namespace std; + ; int S, N; int T[MAX_N], F[MAX_N]; int sum_F[MAX_N ...

  9. 使用zxing二维码识别

    1.多二维码识别 (同一张图片中多二维码识别) 直接上代码舒服: pom文件: <!-- QR Code --> <dependency> <groupId>com ...

  10. python核心编程2 第七章 练习

    7-4. 建立字典. 给定两个长度相同的列表,比如说,列表[1, 2, 3,...]和['abc', 'def','ghi',...],用这两个列表里的所有数据组成一个字典,像这样:{1:'abc', ...