场景:

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 的函数原型.

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

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

/// remove_reference
template<typename _Tp>
struct remove_reference
{ typedef _Tp type; }; template<typename _Tp>
struct remove_reference<_Tp&>
{ typedef _Tp type; }; template<typename _Tp>
struct remove_reference<_Tp&&>
{ typedef _Tp type; };

例子

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

例子1

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

– 摘录自cppreference

void TestSTLObject()
{
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"; }

输出:

After copy, str is "Hello"
After move, str is ""
The contents of the vector are "Hello", "Hello"

例子2

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


#include <iostream>
#include <stdio.h> #include <utility>
#include <vector>
#include <string> class MemoryBlock
{
public: // Simple constructor that initializes the resource.
explicit MemoryBlock(size_t length)
: _length(length)
, _data(new int[length])
{
std::cout << "In MemoryBlock(size_t). length = "
<< _length << "." << std::endl;
} // Destructor.
~MemoryBlock()
{
std::cout << "In ~MemoryBlock(). length = "
<< _length << "."; if (_data != nullptr)
{
std::cout << " Deleting resource.";
// Delete the resource.
delete[] _data;
} std::cout << std::endl;
} // Copy constructor.
MemoryBlock(const MemoryBlock& other)
: _length(other._length)
, _data(new int[other._length])
{
std::cout << "In MemoryBlock(const MemoryBlock&). length = "
<< other._length << ". Copying resource." << std::endl; std::copy(other._data, other._data + _length, _data);
} // Copy assignment operator.
MemoryBlock& operator=(const MemoryBlock& other)
{
std::cout << "In operator=(const MemoryBlock&). length = "
<< other._length << ". Copying resource." << std::endl; if (this != &other)
{
// Free the existing resource.
delete[] _data; _length = other._length;
_data = new int[_length];
std::copy(other._data, other._data + _length, _data);
}
return *this;
} // Retrieves the length of the data resource.
size_t Length() const
{
return _length;
} // Move constructor.
MemoryBlock(MemoryBlock&& other)
: _data(nullptr)
, _length(0)
{
std::cout << "In MemoryBlock(MemoryBlock&&). length = "
<< other._length << ". Moving resource." << std::endl; // Copy the data pointer and its length from the
// source object.
_data = other._data;
_length = other._length; // Release the data pointer from the source object so that
// the destructor does not free the memory multiple times.
other._data = nullptr;
other._length = 0;
} // Move assignment operator.
MemoryBlock& operator=(MemoryBlock&& other)
{
std::cout << "In operator=(MemoryBlock&&). length = "
<< other._length << "." << std::endl; if (this != &other)
{
// Free the existing resource.
delete[] _data; // Copy the data pointer and its length from the
// source object.
_data = other._data;
_length = other._length; // Release the data pointer from the source object so that
// the destructor does not free the memory multiple times.
other._data = nullptr;
other._length = 0;
}
return *this;
} private:
size_t _length; // The length of the resource.
int* _data; // The resource.
}; void TestSTLObject()
{
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"; } void TestMyObjectWithoutUseMove()
{
std::vector<MemoryBlock> v;
MemoryBlock mb1(25);
// MemoryBlock mb2(75);
// MemoryBlock mb3(50); v.push_back(mb1);
//v.push_back(mb2);
//v.insert(v.begin() + 1, mb3);
} void TestMyObjectWithUseMove()
{
std::vector<MemoryBlock> v; MemoryBlock mb1(25);
// MemoryBlock mb2(75);
// MemoryBlock mb3(50); v.push_back(std::move(mb1));
//v.push_back(MemoryBlock(75));
//v.insert(v.begin() + 1, MemoryBlock(50));
} int main(int argc, char const *argv[])
{ //TestSTLObject();
TestMyObjectWithoutUseMove();
std::cout << "......................................." << std::endl;
TestMyObjectWithUseMove();
return 0;
}
输出:
1. 注意,第一个函数每个对象多调用了拷贝构造函数,多创建了一次,而使用了move操作的只是移动了资源
2. 注意,vector即使 push_back 第二个对象时,会移动第一个对象,很奇怪,如果你把注释去掉的话,会发现资源 Moving 很多次,这是 vector 实现影响了, 这个是因为push_back引发vector长度增长导致其内存重新分配,原来vector中的对象都被移动到新分配的内存上,所以会多次调用move构造函数,如果事先reverse,就不会发生这种情况.比较清楚的看出来 Move 的特性的就是 push_back 一个参数.
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.

**转自 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. Monkey测试结果分析【转】

    转自[http://www.douban.com/note/257030241/] Monkey测试结果分析 一. 初步分析方法: Monkey测试出现错误后,一般的差错步骤为以下几步: 1. 找到是 ...

  2. 有关于tomcat启动时,利用listener来执行某个方法

    今天,项目经理让我调查一下(目的是锻炼我),刚开始的时候说用listener来实现服务器启动然后某个项目跟着启动.其实就是tomcat启动的时候去执行某个方法,通过这个方法启动某个项目.我网上调查了一 ...

  3. CentOS添加环境变量

    添加这个两个路径:/usr/local/webserver/python/bin:/usr/local/webserver/mysql/bin 方法一:直接运行命令export PATH=$PATH: ...

  4. hdu2973-YAPTCHA-(欧拉筛+威尔逊定理+前缀和)

    YAPTCHA Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  5. 转载一篇必须超级好的JVM配置实战

    不管是YGC还是Full GC,GC过程中都会对导致程序运行中中断,正确的选择不同的GC策略,调整JVM.GC的参数,可以极大的减少由于GC工作,而导致的程序运行中断方面的问题,进而适当的提高Java ...

  6. java.net.NoRouteToHostException

    1.之前一直默认的一个请求url,后来后台ip更换后,就报上述错误.网上好多方法说:关闭服务器端的防火墙,但试过没用. 问题有待重新测试解决

  7. 动态代理 JDK动态代理 CGLIB代理

    代理模式:代理类和被代理类实现共同的接口(或继承),代理类中存有指向被代理类的索引,实际执行时通过调用代理类的方法.实际执行的是被代理类的方法. 而AOP,是通过动态代理实现的. 一.简单来说: JD ...

  8. Compute Shader

    [Compute Shader] 1.Similar to regular shaders, compute shaders are Asset files in your project, with ...

  9. spring boot IDEA 开发微服务(二)

    https://www.cnblogs.com/spaceud/p/8097934.html 参考以上微博,自己实际操作步骤如下: 1~开发准备 JDK:1.8 Spring Boot:2.1.0 S ...

  10. 使用rsync 同步数据一些常用参数和示例

    rsync rsync是linux系统下的数据镜像备份工具.支持远程同步,本地复制,或者与其他SSH.rsync主机同步. 包括本地推到远程,远程拉到本地两种同步方式,也可以实现本地不同路径下文件的同 ...