stout中大量使用了c++11的特性,而c++11中move和forward大概是最神奇的特性了.

  1. 左值和右值的区别
int a = ;   // a是左值,0是右值
int b = rand(); // b是左值,rand()是右值

  直观理解:左值在等号左边,右值在等号右边

  深入理解:左值有名称,可根据左值获取其内存地址,而右值没有名称,不能根据右值获取地址。

  2. 引用叠加规则

  左值引用A&和右值引用A&& 可相互叠加

A& + A& = A&
A& + A&& = A&
A&& + A& = A&
A&& + A&& = A&&

  举例示例,void foo(T&& x)中,如果T是int&, x为左值语义,如果T是int&&, x为右值语义

 

  3. 为什么要使用std::move

  如果类X包含一个指向某资源的指针,在左值语义下,类X的赋值构造函数如下:

X::X(const X& other)
{
// ....
// 销毁资源
// 复制other的资源,并使指针指向它
// ...
}

  应用代码如下,其中,tmp被赋给a之后,便不再使用。

X tmp;
// ...经过一系列初始化...
X a = tmp;

  如上,执行过程按照时间顺序如下: 首先执行一次默认构造函数(tmp申请资源),再执行一次复制构造函数(a复制资源), 最后退出作用域时再执行一次析构函数(tmp释放资源)。既然tmp迟早要被析构掉,在执行复制构造函数的时候,a能不能将tmp的资源“偷“”过来,直接为我所用?

X::X(const X& other)
{
// 交换this和other的资源
}

  这样可以减少一次资源的创建和释放。这就是std::move所要实现的。

  

  4. std::move的实现

  std::move用于强制将左值转化为右值。其实现方式如下:

template<class T>
typename remove_reference<T>::type&&
std::move(T&& a) noexcept
{
typedef typename remove_reference<T>::type&& RvalRef;
return static_cast<RvalRef>(a);
}

  当a为int左值(右值)时,根据引用叠加原理,T为int&, remove_reference<T> = int, std::move返回类型为int&&,即右值引用

  

  5. std::move的使用

#include <utility>
#include <iostream>
#include <string>
#include <vector> void foo(const std::string& n)
{
std::cout << "lvalue" << std::endl;
} void foo(std::string&& n)
{
std::cout << "rvalue" << std::endl;
} void bar()
{
foo("hello"); // rvalue
std::string a = "world";
foo(a); // lvalue
foo(std::move(a)); // rvalue
} int main()
{
std::vector<std::string> a = {"hello", "world"};
std::vector<std::string> b; b.push_back("hello");
b.push_back(std::move(a[])); std::cout << "bsize: " << b.size() << std::endl;
for (std::string& x: b)
std::cout << x << std::endl;
bar();
return ;
}

  6. 为什么要使用std::forward

  首先看下面这段代码:

#include <utility>
#include <iostream> void bar(const int& x)
{
std::cout << "lvalue" << std::endl;
} void bar(int&& x)
{
std::cout << "rvalue" << std::endl;
} template <typename T>
void foo(T&& x)
{
bar(x);
bar(std::forward<T>(x));
} int main()
{
int x = ;
foo(x);
foo();
return ;
}

  执行foo(10):首先进入函数foo, 执行bar(x), 输出"lvalue"。这里有点不合常理,10明明是一个右值,为什么这里输出"lvalue"呢?这是因为10只是作为一个foo的右值参数,但是在foo函数内部,x却是一个有名字的变量,因此10是bar(x)的左值参数。但是我们想延续10的左值语义,怎么办呢?std::forward就派上了用场。

  总而言之,std::forward的目的就是保持std::move的语意。

  7. std::forwar的实现

template<typename T, typename Arg>
shared_ptr<T> factory(Arg&& arg)
{
return shared_ptr<T>(new T(std::forward<Arg>(arg)));
}
template<class S>
S&& forward(typename remove_reference<S>::type& a) noexcept
{
return static_cast<S&&>(a);
}
X x;
factory<A>(x);

  如果factory的输入参数是一个左值 => Arg = X& => std::forward<Arg> = X&, 这种情况下,std::forward<Arg>(arg)仍然是左值。

  相反,如果factory输入参数是一个右值 => Arg = X => std::forward<Arg> = X, 这种情况下,std::forward<Arg>(arg)是一个右值。

  

  8. std::forward的使用

  直接上码,如果前面都懂了,相信这段代码的输出结果也能猜个八九不离十了。  

#include <utility>
#include <iostream> void overloaded(const int& x)
{
std::cout << "[lvalue]" << std::endl;
} void overloaded(int&& x)
{
std::cout << "[rvalue]" << std::endl;
} template <class T> void fn(T&& x)
{
overloaded(x);
overloaded(std::forward<T>(x));
} int main()
{
int i = ;
overloaded(std::forward<int>(i));
overloaded(std::forward<int&>(i));
overloaded(std::forward<int&&>(i)); fn(i);
fn(std::move(i)); return ;
}

最新文章

  1. 【Win 10 应用开发】在App所在的进程中执行后台任务
  2. https://github.com/chenghuige/tensorflow-exp/blob/master/examples/sparse-tensor-classification/
  3. CentOS6.5下安装JDK
  4. 深入浅出Java回调机制
  5. 轻轻送送为你的App加点特效
  6. Scrambled Polygon---poj2007(利用叉积排序)
  7. 关于 MySQL LEFT JOIN 你可能需要了解的三点
  8. vector(相对线程安全) arryList(线程不安全)
  9. wcf中netTcpBinding的元素构成
  10. html5 文件拖拽上传
  11. 周赛C题 LightOJ 1047 (DP)
  12. C# 中根据datetime的值来计算属于本年的第几周,类似delphi中的weekoftheyear功能
  13. iOS的GIF动画效果实现
  14. 2018上C语言程序设计(高级)- 第0次作业成绩
  15. SpringBoot+mybatis使用@Transactional无效
  16. python基础学习(十三)函数进阶
  17. 在centos7上编译安装nginx
  18. Scrapy突破反爬虫的限制
  19. ubuntu下tensorflow 报错 libcusolver.so.8.0: cannot open shared object file: No such file or directory
  20. Java基础(basis)-----抽象类和接口详解

热门文章

  1. Bitcoin: A Peer-to-Peer Electronic Cash System
  2. STM32F4编程手册学习2_内存模型
  3. 2018Java研发实习内推
  4. js中的数组对象排序
  5. 基于spec评论“欢迎来怼”团队Alpha版作品
  6. Daily Scrum 9
  7. dataTables基础函数变量
  8. 内存转储文件调试系统崩溃bug
  9. python-网易云简单爬虫
  10. GPS定位,根据经纬度查询附近地点的经纬度-sql方法实现