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. 推荐一个Chrome扩展应用,能够自动去除CSDN广告

    作为一个程序员,每天编程遇到问题时,少不了前往国内著名的CSDN网站上查信息,看是否有同行遇到类似问题.很多时候根据遇到问题的错误消息进行搜索,结果都是一篇篇CSDN博客.这些博客打开后都会显示很多广 ...

  2. 课堂笔记——循环语句-for

    一.循环:多次执行某段代码. 二.循环四要素: 1.初始条件 2.循环条件 3.状态改变 4.循环体 三.for循环 1.语法: for(初始条件;循环条件;状态改变)       { 循环体 } 2 ...

  3. 取火柴游戏||Nim博弈

    好久之前看的sg函数了 好像就记住一个nim博弈qwq 第一次啊看的时候很迷,现在感觉可以了qwq 首先我们来看一个其他的游戏.(以下游戏只有两个人参与,且足够聪明) 两个人在一张圆形的桌子上放等大的 ...

  4. 封装方法到对象(javascript)

    /*! * artDialog 5 * Date: 2012-03-21 * http://code.google.com/p/artdialog/ * (c) 2009-2012 TangBin, ...

  5. C#结构体和字节数组的转换函数

    在通信过程中,一般我们都会操作到字节数组.特别是希望在不同语言编程进行操作的时候. 虽然C#提供了序列化的支持,不用字节数组也行.但操作字节数组肯定会碰到.   一般都会采用结构来表示字节数组.但结构 ...

  6. 简单实用的.htaccess文件配置

    .htaccess 文件 (Hypertext Access file) 是Apache Web服务器的一个非常强大的配置文件,对于这个文件,Apache有一堆参数可以让你配置出几乎随心所欲的功能.. ...

  7. css的基础用法(上)

    css定义: CSS层叠式样表(Cascading  Style Sheets)是一种用来表现html或xml等文件样式的计算机语言.CSS不仅可以静态的修饰网页,还可以配合各种脚本语言动态地对网页个 ...

  8. [Linux/Unix]常用命令

    1.查看日志后200行有“TNS字符串的详细信息 file_name |grep TNS 实时查看文件情况: tail -f file_name 2.查看路由情况: #Linux环境 tracerou ...

  9. NEC html规范

    HTML规范 - 整体结构 HTML基础设施 文件应以“<!DOCTYPE ......>”首行顶格开始,推荐使用“<!DOCTYPE html>”. 必须申明文档的编码cha ...

  10. Linux 学习第二天

    一.常用命令的使用 1.echo echo https://www.cnblogs.com/rise-home/ 输出字符串 2.ps  -aux(显示进程信息) ps 进程状态共有5种 A.STAT ...