当前位置: 首页 > news >正文

张家港外贸型网站制作自助建站系统下载

张家港外贸型网站制作,自助建站系统下载,罗湖网站建设的公司,网站加速代码前言 有一节我们分析了std::bind的实现原理,本节稍作休息分析一个比较简单的std::move 原理 std::move的原理太简单了,一句话:就是把传进来的参数强转为右值引用类型。作用为调用移动构造函数,或移动赋值函数。下面通过例子和代…

前言

有一节我们分析了std::bind的实现原理,本节稍作休息分析一个比较简单的std::move

原理

std::move的原理太简单了,一句话:就是把传进来的参数强转为右值引用类型。作用为调用移动构造函数,或移动赋值函数。下面通过例子和代码说明。

例子

#include <iostream>
#include <cstring>using namespace std;struct Thing {Thing(){cout << "default construct\n";}// Copy operatorThing(const Thing& other){cout << "copy constructor\n";}// Move constructorThing(Thing&& other) noexcept{cout << "move constructor\n";}// assignment operatorThing& operator=(const Thing& rhs) {cout << "copy operator\n";return *this;}// move assignment operatorThing& operator=(Thing&& rhs) noexcept {cout << "move operator\n";return *this;}// destructor necessary since we are working in dangerous new/delete territory~Thing() noexcept {cout << "destructor " << "\n";}
};
int main()
{cout << "main::constructing a\n";Thing a;cout << "main::moving a to newly constructed c\n";Thing c(std::move(a));Thing x = std::move(c);cout << ">main::thing y\n";Thing y;y = std::move(x);return 0;
}

[mzhai@c++11]$ ./a.out
main::constructing a
default construct
main::moving a to newly constructed c
move constructor //Thing c(std::move(a));
move constructor //Thing x = std::move©;
main::thing y
default construct
move operator //y = std::move(x);
destructor
destructor
destructor
destructor

a, c, x虽然都是左值,但std::move却把它们强转成了右值引用,从而调用了move语义的函数而不是copy语义的。

std::move源码

 92   /**93    *  @brief  Convert a value to an rvalue.94    *  @param  __t  A thing of arbitrary type.95    *  @return The parameter cast to an rvalue-reference to allow moving it.96   */97   template<typename _Tp>98     constexpr typename std::remove_reference<_Tp>::type&&99     move(_Tp&& __t) noexcept
100     { return static_cast<typename std::remove_reference<_Tp>::type&&>(__t); }

让我们看看remove_reference 的定义,看看它是怎么脱去类型的外衣的:

1402   /// remove_reference
1403   template<typename _Tp>
1404     struct remove_reference
1405     { typedef _Tp   type; };
1406
1407   template<typename _Tp>
1408     struct remove_reference<_Tp&>
1409     { typedef _Tp   type; };
1410
1411   template<typename _Tp>
1412     struct remove_reference<_Tp&&>
1413     { typedef _Tp   type; };

原来不管你原来的类型是左值引用还是右值引用,统统都定义type为脱去外衣的类型。

http://www.dinnco.com/news/24806.html

相关文章:

  • 网站做动态和静态哪个贵微信小程序官网
  • 创意网站关于seo如何优化
  • 软件开发网站武汉网站收录入口
  • 成都网站建设哪个好特大新闻凌晨刚刚发生
  • 北京网站建设公司制作网站cba最新消息
  • 佛山优化网站方法找客户的软件有哪些
  • 将自己做的网站用电脑发到网上网络营销企业是什么
  • 酒店网站建设背景分析推广关键词怎么设置
  • 深圳的互联网公司有哪些北京seo公司公司
  • 杭州 专业网站建设 网络服务百度推广登录首页网址
  • 公司企业网站建设多少钱怎么在百度上打广告
  • 邢台做wap网站的地方百度网盘资源搜索引擎搜索
  • 2个女人做暧暧网站广东vs北京首钢
  • 珠海做网站的公司有哪些怎么发外链
  • 求一个网站推广引流吸引人的标题
  • 电商网站的活动怎么做西安网站制作推广
  • 护肤品网站建设的意义seo关键词推广
  • 爱站网关键词seo查询工具
  • 苏州 建设中心网站网站百度关键词排名软件
  • 企业建设网站个人总结培训学校
  • 武汉做网站要多少钱安卓系统优化软件
  • 聊城做网站低费用手机网站制作
  • asp网站用什么做西安全网优化
  • 自己做网站卖东西可以今天国际新闻最新消息10条
  • 网站建设方案书模板网络广告销售
  • 工程承包网站哪个好?中文搜索引擎排行榜
  • 民权网站建设百度云登录入口
  • 在哪个网站做兼职淘宝客服seo怎么优化步骤
  • 怎么做网站 ppt互联网seo是什么意思
  • wordpress 影楼主题seo搜索优化公司报价