场景:

C++ 标准库使用比如vector::push_back 等这类函数时,会对参数的对象进行复制,连数据也会复制.这就会造成对象内存的额外创建, 本来原意是想把参数push_back进去就行了.

C++11 提供了std::move 函数来把左值转换为xrvalue, 而且新版的push_back也支持&&参数的重载版本,这时候就可以高效率的使用内存了.

对指针类型的标准库对象并不需要这么做.

参考:

  1. Move Constructors and Move Assignment Operators (C++)
  2. std::move

说明:

std::move(t) 用来表明对象t 是可以moved from的,它允许高效的从t资源转换到lvalue上.

注意,标准库对象支持moved from的左值在moved 之后它的对象原值是有效的(可以正常析构),但是是unspecified的,可以理解为空数据,但是这个对象的其他方法返回值不一定是0,比如size().所以,moved from 之后的对象最好还是不要使用吧?(如有不正确理解,请告知)

对本身进行move,并赋值给本身是undefined的行为.

std::vector v = {2, 3, 3};

v = std::move(v); // undefined behavior

std::move 的函数原型.

  1. template<typename _Tp>
  2. constexpr typename std::remove_reference<_Tp>::type&&
  3. move(_Tp&& __t) noexcept
  4. { return static_cast<typename std::remove_reference<_Tp>::type&&>(__t); }

结构体 remove_reference 的原型,就是重载了多个结构体模板来获取原类型 type.

  1. /// remove_reference
  2. template<typename _Tp>
  3. struct remove_reference
  4. { typedef _Tp type; };
  5. template<typename _Tp>
  6. struct remove_reference<_Tp&>
  7. { typedef _Tp type; };
  8. template<typename _Tp>
  9. struct remove_reference<_Tp&&>
  10. { typedef _Tp type; };

例子

以下用两个例子来说明std::move的用法.

例子1

– 原lvalue值被moved from之后值被转移,所以为空字符串.

– 摘录自cppreference

  1. void TestSTLObject()
  2. {
  3. std::string str = "Hello";
  4. std::vector<std::string> v;
  5. // uses the push_back(const T&) overload, which means
  6. // we'll incur the cost of copying str
  7. v.push_back(str);
  8. std::cout << "After copy, str is \"" << str << "\"\n";
  9. // uses the rvalue reference push_back(T&&) overload,
  10. // which means no strings will be copied; instead, the contents
  11. // of str will be moved into the vector. This is less
  12. // expensive, but also means str might now be empty.
  13. v.push_back(std::move(str));
  14. std::cout << "After move, str is \"" << str << "\"\n";
  15. std::cout << "The contents of the vector are \"" << v[0]
  16. << "\", \"" << v[1] << "\"\n";
  17. }

输出:

  1. After copy, str is "Hello"
  2. After move, str is ""
  3. The contents of the vector are "Hello", "Hello"

例子2

– 自定义自己的类对象支持moved from 操作,需要实现 Move Constructors and Move Assignment Operators


  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <utility>
  4. #include <vector>
  5. #include <string>
  6. class MemoryBlock
  7. {
  8. public:
  9. // Simple constructor that initializes the resource.
  10. explicit MemoryBlock(size_t length)
  11. : _length(length)
  12. , _data(new int[length])
  13. {
  14. std::cout << "In MemoryBlock(size_t). length = "
  15. << _length << "." << std::endl;
  16. }
  17. // Destructor.
  18. ~MemoryBlock()
  19. {
  20. std::cout << "In ~MemoryBlock(). length = "
  21. << _length << ".";
  22. if (_data != nullptr)
  23. {
  24. std::cout << " Deleting resource.";
  25. // Delete the resource.
  26. delete[] _data;
  27. }
  28. std::cout << std::endl;
  29. }
  30. // Copy constructor.
  31. MemoryBlock(const MemoryBlock& other)
  32. : _length(other._length)
  33. , _data(new int[other._length])
  34. {
  35. std::cout << "In MemoryBlock(const MemoryBlock&). length = "
  36. << other._length << ". Copying resource." << std::endl;
  37. std::copy(other._data, other._data + _length, _data);
  38. }
  39. // Copy assignment operator.
  40. MemoryBlock& operator=(const MemoryBlock& other)
  41. {
  42. std::cout << "In operator=(const MemoryBlock&). length = "
  43. << other._length << ". Copying resource." << std::endl;
  44. if (this != &other)
  45. {
  46. // Free the existing resource.
  47. delete[] _data;
  48. _length = other._length;
  49. _data = new int[_length];
  50. std::copy(other._data, other._data + _length, _data);
  51. }
  52. return *this;
  53. }
  54. // Retrieves the length of the data resource.
  55. size_t Length() const
  56. {
  57. return _length;
  58. }
  59. // Move constructor.
  60. MemoryBlock(MemoryBlock&& other)
  61. : _data(nullptr)
  62. , _length(0)
  63. {
  64. std::cout << "In MemoryBlock(MemoryBlock&&). length = "
  65. << other._length << ". Moving resource." << std::endl;
  66. // Copy the data pointer and its length from the
  67. // source object.
  68. _data = other._data;
  69. _length = other._length;
  70. // Release the data pointer from the source object so that
  71. // the destructor does not free the memory multiple times.
  72. other._data = nullptr;
  73. other._length = 0;
  74. }
  75. // Move assignment operator.
  76. MemoryBlock& operator=(MemoryBlock&& other)
  77. {
  78. std::cout << "In operator=(MemoryBlock&&). length = "
  79. << other._length << "." << std::endl;
  80. if (this != &other)
  81. {
  82. // Free the existing resource.
  83. delete[] _data;
  84. // Copy the data pointer and its length from the
  85. // source object.
  86. _data = other._data;
  87. _length = other._length;
  88. // Release the data pointer from the source object so that
  89. // the destructor does not free the memory multiple times.
  90. other._data = nullptr;
  91. other._length = 0;
  92. }
  93. return *this;
  94. }
  95. private:
  96. size_t _length; // The length of the resource.
  97. int* _data; // The resource.
  98. };
  99. void TestSTLObject()
  100. {
  101. std::string str = "Hello";
  102. std::vector<std::string> v;
  103. // uses the push_back(const T&) overload, which means
  104. // we'll incur the cost of copying str
  105. v.push_back(str);
  106. std::cout << "After copy, str is \"" << str << "\"\n";
  107. // uses the rvalue reference push_back(T&&) overload,
  108. // which means no strings will be copied; instead, the contents
  109. // of str will be moved into the vector. This is less
  110. // expensive, but also means str might now be empty.
  111. v.push_back(std::move(str));
  112. std::cout << "After move, str is \"" << str << "\"\n";
  113. std::cout << "The contents of the vector are \"" << v[0]
  114. << "\", \"" << v[1] << "\"\n";
  115. }
  116. void TestMyObjectWithoutUseMove()
  117. {
  118. std::vector<MemoryBlock> v;
  119. MemoryBlock mb1(25);
  120. // MemoryBlock mb2(75);
  121. // MemoryBlock mb3(50);
  122. v.push_back(mb1);
  123. //v.push_back(mb2);
  124. //v.insert(v.begin() + 1, mb3);
  125. }
  126. void TestMyObjectWithUseMove()
  127. {
  128. std::vector<MemoryBlock> v;
  129. MemoryBlock mb1(25);
  130. // MemoryBlock mb2(75);
  131. // MemoryBlock mb3(50);
  132. v.push_back(std::move(mb1));
  133. //v.push_back(MemoryBlock(75));
  134. //v.insert(v.begin() + 1, MemoryBlock(50));
  135. }
  136. int main(int argc, char const *argv[])
  137. {
  138. //TestSTLObject();
  139. TestMyObjectWithoutUseMove();
  140. std::cout << "......................................." << std::endl;
  141. TestMyObjectWithUseMove();
  142. return 0;
  143. }
  1. 输出:
  2. 1. 注意,第一个函数每个对象多调用了拷贝构造函数,多创建了一次,而使用了move操作的只是移动了资源
  3. 2. 注意,vector即使 push_back 第二个对象时,会移动第一个对象,很奇怪,如果你把注释去掉的话,会发现资源 Moving 很多次,这是 vector 实现影响了, 这个是因为push_back引发vector长度增长导致其内存重新分配,原来vector中的对象都被移动到新分配的内存上,所以会多次调用move构造函数,如果事先reverse,就不会发生这种情况.比较清楚的看出来 Move 的特性的就是 push_back 一个参数.
  4. 3. 注意,g++ 4.8.1 vector push_back 多个对象时优化的没 vs 好,vs 是调用 Move 构造器,而 g++ 是调用 Copy 构造器,你会发现拷贝构造函数会调用很多次.

In MemoryBlock(size_t). length = 25.

In MemoryBlock(const MemoryBlock&). length = 25. Copying resource.

In ~MemoryBlock(). length = 25. Deleting resource.

In ~MemoryBlock(). length = 25. Deleting resource.

.......................................

In MemoryBlock(size_t). length = 25.

In MemoryBlock(MemoryBlock&&). length = 25. Moving resource.

In ~MemoryBlock(). length = 0.

In ~MemoryBlock(). length = 25. Deleting resource.

  1. **转自 https://blog.csdn.net/infoworld/article/details/50736633**

std::move 移动对象资源的更多相关文章

  1. C++ 11 右值引用以及std::move

    转载请注明出处:http://blog.csdn.net/luotuo44/article/details/46779063 新类型: int和int&是什么?都是类型.int是整数类型,in ...

  2. C++ 11 左值,右值,左值引用,右值引用,std::move, std::foward

    这篇文章要介绍的内容和标题一致,关于C++ 11中的这几个特性网上介绍的文章很多,看了一些之后想把几个比较关键的点总结记录一下,文章比较长.给出了很多代码示例,都是编译运行测试过的,希望能用这些帮助理 ...

  3. 右值引用和std::move函数(c++11)

    1.对象移动 1)C++11新标准中的一个最主要的特性就是移动而非拷贝对象的能力 2)优势: 在某些情况下,从旧内存拷贝到新内存是不必要的,此时对对象进行移动而非拷贝可以提升性能 有些类如IO类或un ...

  4. [转载]如何在C++03中模拟C++11的右值引用std::move特性

    本文摘自: http://adamcavendish.is-programmer.com/posts/38190.htm 引言 众所周知,C++11 的新特性中有一个非常重要的特性,那就是 rvalu ...

  5. 移动构造和移动赋值与std::move

    ------------------------------------移动构造------------------------------------------ 传统的深拷贝深赋值 对于类中,含有 ...

  6. 左值 lvalue,右值 rvalue 和 移动语义 std::move

    参考文章: [1] 基础篇:lvalue,rvalue和move [2] 深入浅出 C++ 右值引用 [3] Modern CPP Tutorial [4] 右值引用与转移语义 刷 Leetcode ...

  7. C++ 11中的右值引用以及std::move

    看了很多篇文章,现在终于搞懂了C++ 中的右值以及std::move   左值和右值最重要的区别就是右值其实是一个临时的变量 在C++ 11中,也为右值引用增加了新语法,即&&   比 ...

  8. C++11右值引用和std::move语句实例解析

    关键字:C++11,右值引用,rvalue,std::move,VS 2015 OS:Windows 10 右值引用(及其支持的Move语意和完美转发)是C++0x将要加入的最重大语言特性之一.从实践 ...

  9. c++ 11 移动语义、std::move 左值、右值、将亡值、纯右值、右值引用

    为什么要用移动语义 先看看下面的代码 // rvalue_reference.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #includ ...

随机推荐

  1. Ajax 学习 第二篇

    XMLHttpRequest发送请求 open(method,url,async) 解释 method:do/post,不区分大小写 URL:相对地址 文档地址 async:默认为TRUE 即异步 F ...

  2. pandas数据操作

    pandas数据操作 字符串方法 Series对象在其str属性中配备了一组字符串处理方法,可以很容易的应用到数组中的每个元素 t = pd.Series(['a_b_c_d','c_d_e',np. ...

  3. BOS物流项目第十二天

    教学计划 1.角色管理 a.  添加角色功能 b.  角色分页查询 2.用户管理 a.  添加用户功能 b.  用户分页查询 3.修改Realm中授权方法(查询数据库) 4.使用ehcache缓存权限 ...

  4. Jenkins 踩过的坑之再总结

    在安装完jenkins后,linux中默认使用的jenkins这个用户,这时在构建完项目后我们需要执行一些shell命令时会出现没有权限的情况,导致构建失败,这里我们需要给jenkins用户相应的权限 ...

  5. keras初探

    1.对网络的理解: 2.怎样训练,输入已经数据,经过训练,输入测试数据,得到相似数据 3.RNNs  循环神经网络

  6. Python教程_简介2

    人生苦短,我用Python--Life is short,you need Python. https://www.bilibili.com/video/av14184325/?p=101 Pytho ...

  7. CentOS 6.5下Redis安装测试

    NoSQL之Redis - CentOS 6.5安装测试 1.下载redis 可以在线安装或者下载 redis ①在线安装前需要检测是否存在rpm包不存在的话查看yum在线是否存在rpm包不存在的话就 ...

  8. PHPActiveRecord 学习一

    #连接数据库 <?phprequire_once dirname(__FILE__) . '/../../ActiveRecord.php'; // initialize ActiveRecor ...

  9. 修改app工程名 Android Studio

    1.关掉AndroidStudio,在原项目最外层文件夹和内部xxx.iml上直接重新命名, 2.然后重新打开AndroidStudio,加载项目, 3.最后

  10. centos 6.9 +nginx 配置GIT HTTPS服务器(证书采用自签名)

    第一部分原通过SSH访问的GIT服务器迁移 1.把原服务器GIT资源库目录完成复制至新的服务器 2.安装GIT服务器 新的服务器 创建用户 useradd git password git 下载GIT ...