关于C++11新特性之std::move、std::forward、左右值引用网上资料已经很多了,我主要针对测试性能做一个测试,梳理一下这些逻辑,首先,左值比较熟悉,右值就是临时变量,意味着使用一次就不会再被使用了。针对这两种值引入了左值引用和右值引用,以及引用折叠的概念。

1.右值引用的举例测试

#include <iostream>
using namespace std;

//创建一个测试类
class A
{
public:
A() : m_a()
{
}

int m_a;
};

void funcA(A&& param) // 右值引用参数,只接受右值
{
cout << param.m_a << endl; // param与a的地址一致,仅仅只是取了一个新名字
}

int main()
{
A a;
funcA(move(a)); //必须将其转换为右值
cout << a.m_a << endl; //正常打印,所以std::move并没有移动的能力
return ;
}

2.左值和右值引用的举例测试,以及引出万能引用

构造一组重载函数,分别接受右值,和左值的参数,还有const A&的参数重载函数。

void funcA(const A& param)//既可以接受右值引用,也可以接受左值引用,但是有一个隐式转换const A&
void funcA(A& param)// 接受左值引用
void funcA(A&& param) // 接受右值引用

const A& param既可以接受右值引用,也可以接受左值引用,但是存在一个隐式转换,const使用受限制。

#include <iostream>
using namespace std;

//创建一个测试类
class A
{
public:
A() : m_a() // 构造函数
{
cout << "Constructor" << endl;
}
A(const A & other) : m_a() // copy构造函数
{
cout << "Copy Constructor" << endl;
if (this == &other)
{
return;
}
this->m_a = other.m_a;
}
A& operator=(const A& other) // 赋值构造函数
{
cout << "= Constructor" << endl;
if (this == &other)
{
return *this;
}
this->m_a = other.m_a;
return *this;
}
int m_a;
};
void test(A&& pa) //测试是否为右值
{
cout << "只接受右值" << endl;
}
void funcA(const A& param) // 既可以接受右值引用,也可以接受左值引用,但是有一个隐式转换const A&
{
//test(param); //编译不过,param可以接受右值,但是param被转换为const左值
//test(std::forward<A>(param)); //编译不过,param可以接受右值,但是param被转换为const左值
cout << param.m_a << endl;
}
void funcA(A& param) // 接受左值引用
{
//test(param); //编译不过,param可以接受右值,但是param被转换为左值
test(std::forward<A>(param)); //编译通过,通过forward转发
cout << param.m_a << endl;
}
void funcA(A&& param) // 接受右值引用
{
//test(param); //编译不过,param被转换为左值
test(std::forward<A>(param)); //编译通过,通过forward转发
cout << param.m_a << endl;
}

int main()
{
A a;
const A& b = a;
funcA(a);
funcA(move(a));
funcA(b);
cout << a.m_a << endl; //正常打印,所以std::move并没有移动的能力
return ;
}

对此C++11引入了万能引用的概念,使得不需要那么多的重载函数,既可以接受右值引用,也可以接受左值引用。但是函数内部,再需要调用一个左值或者右值的函数时,我们就得需要forward模版类。

#include <iostream>
using namespace std;

//创建一个测试类
class A
{
public:
A() : m_a(new int()) // 构造函数
{
cout << "Constructor" << endl;
}
A(const A & other) : m_a(new int()) // copy构造函数
{
cout << "Copy Constructor" << endl;
if (this == &other)
return;
this->m_a = other.m_a;
}
A& operator=(const A& other) // 赋值构造函数
{
cout << "= Constructor" << endl;
if (this == &other)
return *this;

this->m_a = other.m_a;
return *this;
}
int* m_a;
};
void test(A&& pa) //测试是否为右值
{
cout << "只接受右值" << endl;
}
void test(A& pa) //测试是否为左值
{
cout << "只接受左值" << endl;
}

template<class T>
void funcA(T&& param)
{
test(std::forward<T>(param)); //编译通过,通过forward完美转发
cout << *param.m_a << endl;
}

int main()
{
A a;
funcA(a);
funcA(move(a));
cout << *a.m_a << endl; //正常打印,所以std::move并没有移动的能力
return ;
}

3.移动构造函数的引出

以上的所有特性,所能体现出来的是我们对于临时变量的使用,尽可能的使用中间生成的临时变量,提高性能,所谓的榨取最后的性能。移动构造函数注意的两点

1.调用移动构造函数时参数(被移动者)必须是右值。

2.调用移动构造函数后被移动者就不能再被使用。

#include <iostream>
using namespace std;

//创建一个测试类
class A
{
public:
A() : m_a(new int()) // 构造函数
{
cout << "Constructor" << endl;
}
A(const A & other) : m_a(new int()) // copy构造函数
{
cout << "Copy Constructor" << endl;
if (this == &other)
{
return;
}
this->m_a = other.m_a;
}
A& operator=(const A& other) // 赋值构造函数
{
cout << "= Constructor" << endl;
if (this == &other)
{
return *this;
}
this->m_a = other.m_a;
return *this;
}

A(A&& other) : m_a(other.m_a) // 移动构造函数,参数是一个右值,
{
cout << "Move Constructor" << endl;
if (this == &other)
{
return;
}
other.m_a = nullptr; //移动后将被移动的对象数据清空
}

int* m_a;
};
void test(A&& pa) //测试是否为右值
{
cout << "只接受右值" << endl;
}
void test(A& pa) //测试是否为左值
{
cout << "只接受左值" << endl;
}

template<class T>
void funcA(T&& param)
{
test(std::forward<T>(param)); //编译通过,通过forward完美转发
cout << *param.m_a << endl;
}

int main()
{
A a;
funcA(a);
funcA(move(a));
A b(move(a)); //调用移动构造函数,新的对象是b对象
cout << *a.m_a << endl; //数据已被移动,程序崩溃
return ;
}

移动构造函数一定程度上较少了临时内存的申请,减少不必要的拷贝,节省了空间和时间。以上特性在使用中还有很多需要注意的地方,如果我遇到了会及时的添加到这里,分享给大家,一起加油。

C++11中std::move、std::forward、左右值引用、移动构造函数的测试的更多相关文章

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

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

  2. c++11 中的 move 与 forward

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

  3. C++11中提供了std::bind

    再来看看std::bind C++11中提供了std::bind.bind()函数的意义就像它的函数名一样,是用来绑定函数调用的某些参数的. bind的思想实际上是一种延迟计算的思想,将可调用对象保存 ...

  4. (原创)C++11改进我们的程序之右值引用

    本次主要讲c++11中的右值引用,后面还会讲到右值引用如何结合std::move优化我们的程序. c++11增加了一个新的类型,称作右值引用(R-value reference),标记为T & ...

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

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

  6. C++11中如何输出enum class的值

    Unlike an unscoped enumeration, a scoped enumeration is not implicitly convertible to its integer va ...

  7. C++11新特性(1) 右值引用

    在C++中,左值(lvalue)是能够获取其地址的一个量.因为常常出如今赋值语句的左边.因此称之为左值.比如一个有名称的变量. 比如: int a=10; //a就是一个左值. 传统的C++引用,都是 ...

  8. move语义和右值引用

    C++11支持move语义,用以避免非必要拷贝和临时对象. 具体内容见收藏中的“C++右值引用” .

  9. 透彻理解C++11新特性:右值引用、std::move、std::forward

    目录 浅拷贝.深拷贝 左值.右值 右值引用类型 强转右值 std::move 重新审视右值引用 右值引用类型和右值的关系 函数参数传递 函数返还值传递 万能引用 引用折叠 完美转发 std::forw ...

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

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

随机推荐

  1. LInux下Posix的传统线程示例

    简介 Linux线程是需要连接pthreat库,线程的使用比进程更灵活,需要注意的是线程间的互斥,或者说是资源共享问题. C++11之后,C++标准库也引入了线程,并且使用非常方便,以后再介绍,这里先 ...

  2. Qt实现的多菜单选择界面

    文章目录 1.效果展示 2.实现代码 2.1 菜单实现代码 2.1.1 头文件 2.1.2 源文件 2.2 应用代码 1.效果展示 这种菜单样式比较常用,实现的方法也有很多种,比如可以直接使用QTab ...

  3. react 样式冲突解决方案 styled-components

    前置 在 react 中解决组件样式冲突的方案中,如果您喜欢将 css 与 js 分离,可能更习惯于 CSS-Modules:如果习惯了 Vue.js 那样的单文件组件,可能习惯于使用 styled- ...

  4. SQL Server中的集合运算: UNION, EXCEPT和INTERSECT

    SQL Server中的集合运算包括UNION(合并),EXCEPT(差集)和INTERSECT(相交)三种. 集合运算的基本使用 1.UNION(合并两个查询结果集,隐式DINSTINCT,删除重复 ...

  5. CUDA线程、线程块、线程束、流多处理器、流处理器、网格概念的深入理解

    一.与CUDA相关的几个概念:thread,block,grid,warp,sp,sm. sp: 最基本的处理单元,streaming processor  最后具体的指令和任务都是在sp上处理的.G ...

  6. C#LeetCode刷题之#110-平衡二叉树(Balanced Binary Tree)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4074 访问. 给定一个二叉树,判断它是否是高度平衡的二叉树. 本 ...

  7. geth建立私链以及发布第一个智能合约

    原博客地址 https://blog.csdn.net/qq_36124194/article/details/83686740 geth建立私链 初始化genesis.json文件 geth --d ...

  8. 1.MongoDB 2.7主从复制(master &ndash;> slave)环境基于时间点的恢复

    (一)MongoDB恢复概述 对于任何类型的数据库,如果要将数据库恢复到过去的任意时间点,否需要有过去某个时间点的全备+全备之后的重做日志,MongoDB也不例外.使用全备将数据库恢复到固定时刻,然后 ...

  9. 一线大厂工程师推荐:Mysql、Springboot、JVM、Spring等面试合集

    前两天晚上,正当我加班沉浸在敲代码的快乐中时,听到前桌的同事在嘀咕:Spring究竟是如何解决的循环依赖? 这让我想起最开始学Java的时候,掌握了一点基本语法和面向对象的一点皮毛.当时心里也是各种想 ...

  10. Android 使用AS编译出错:找不到xx/desugar/debug/66.jar (系统找不到指定的文件。)

    以为是合作人配置文件的问题,后发现是缓存的问题,只需要Clean project,即可. 若提示无法删除目录:Unable to delete directory,或许是因为你打开了另一个项目,只需关 ...