我们也要时刻清醒,有时候右值会转为左值,左值会转为右值。 
(也许“转换”二字用的不是很准确)

如果我们要避免这种转换呢? 
我们需要一种方法能按照参数原来的类型转发到另一个函数中,这才完美,我们称之为完美转发。

std::forward就可以保存参数的左值或右值特性。

因为是这样描述的: 
When used according to the following recipe in a function template, forwards the argument to another function with the value category it had when passed to the calling function.

例子:

template<class T>
void wrapper(T&& arg)
{
foo(std::forward<T>(arg)); // Forward a single argument.
}
template<class T>
void wrapper(T&& arg)
{
foo(std::forward<T>(arg)); // Forward a single argument.
}

If a call to wrapper() passes an rvalue std::string, then T is deduced to std::string (not std::string&, const std::string&, or std::string&&), and std::forward ensures that an rvalue reference is passed to foo. 
If a call to wrapper() passes a const lvalue std::string, then T is deduced to const std::string&, and std::forward ensures that a const lvalue reference is passed to foo. 
If a call to wrapper() passes a non-const lvalue std::string, then T is deduced to std::string&, and std::forward ensures that a non-const lvalue reference is passed to foo.

看一段网站上的代码(http://en.cppreference.com/w/cpp/utility/forward):

#include <iostream>
#include <memory>
#include <utility>
#include <array> struct A {
A(int&& n) { std::cout << "rvalue overload, n=" << n << "\n"; }
A(int& n) { std::cout << "lvalue overload, n=" << n << "\n"; }
}; class B {
public:
template<class T1, class T2, class T3>
B(T1&& t1, T2&& t2, T3&& t3) :
a1_{std::forward<T1>(t1)},
a2_{std::forward<T2>(t2)},
a3_{std::forward<T3>(t3)}
{
} private:
A a1_, a2_, a3_;
}; template<class T, class U>
std::unique_ptr<T> make_unique1(U&& u)
{
return std::unique_ptr<T>(new T(std::forward<U>(u)));
} template<class T, class... U>
std::unique_ptr<T> make_unique(U&&... u)
{
return std::unique_ptr<T>(new T(std::forward<U>(u)...));
} int main()
{
auto p1 = make_unique1<A>(); // rvalue
int i = ;
auto p2 = make_unique1<A>(i); // lvalue std::cout << "B\n";
auto t = make_unique<B>(, i, );
}
//输出:
rvalue overload, n=
lvalue overload, n=
B
rvalue overload, n=
lvalue overload, n=
rvalue overload, n=

最后,记住: 
不管是T&&、左值引用、右值引用,std::forward都会按照原来的类型完美转发。

可能还不是很清楚,再举个栗子

#include <iostream>
using namespace std; void F(int a) {
cout << a << endl;
} void F(int&& a) {    //int&&并不是右值了,只是它能被右值初始化,记住右值引用a是一个绑定了右值对象的左值
// do something
} template<class A>
void G(A &&a) {
return F(std::forward<A>(a)); //
return F(a); //
} int main() {
int i = ;
G(i);
G(); system("pause");
}

例子中,如果你不使用std::forward转发,G中将始终调用void F(int)这个版本,即使G的参数是个右值也不会调用void F(int&& a)

C++11新特性之 std::forward(完美转发)的更多相关文章

  1. C++11新特性之 std::forward(完美转发)(转)

    我们也要时刻清醒,有时候右值会转为左值,左值会转为右值. (也许“转换”二字用的不是很准确) 如果我们要避免这种转换呢? 我们需要一种方法能按照参数原来的类型转发到另一个函数中,这才完美,我们称之为完 ...

  2. C++11新特性 变参模板、完美转发(简述)

    变参模板 (Variadic Template) - 使得 emplace 可以接受任意参数,这样就可以适用于任意对象的构建 完美转发 - 使得接收下来的参数 能够原样的传递给对象的构造函数,这带来另 ...

  3. C++ 11新特性:std::future & std::shared_future) (转载)

    上一讲<C++11 并发指南四(<future> 详解二 std::packaged_task 介绍)>主要介绍了 <future> 头文件中的 std::pack ...

  4. C++ 11新特性:std bind 原理简单图解(转载)

    本文解释了bind 是如何工作的.为了清晰,我对图中的语法作了一些简化(例如,省略函数调用操作符的参数类型),并且简化了 bind 的实现. bind 可以用来将用户提供的需要一个参数的函数转换成不需 ...

  5. C++11新特性应用--实现延时求值(std::function和std::bind)

    说是延时求值,注意还是想搞一搞std::function和std::bind. 之前博客<C++11新特性之std::function>注意是std::function怎样实现回调函数. ...

  6. [转载] C++11新特性

    C++11标准发布已有一段时间了, 维基百科上有对C++11新标准的变化和C++11新特性介绍的文章. 我是一名C++程序员,非常想了解一下C++11. 英文版的维基百科看起来非常费劲,而中文版维基百 ...

  7. c++ 11 线程池---完全使用c++ 11新特性

    前言: 目前网上的c++线程池资源多是使用老版本或者使用系统接口实现,使用c++ 11新特性的不多,最近研究了一下,实现一个简单版本,可实现任意任意参数函数的调用以及获得返回值. 0 前置知识 首先介 ...

  8. C++ 11学习和掌握 ——《深入理解C++ 11:C++11新特性解析和应用》读书笔记(一)

    因为偶然的机会,在图书馆看到<深入理解C++ 11:C++11新特性解析和应用>这本书,大致扫下,受益匪浅,就果断借出来,对于其中的部分内容进行详读并亲自编程测试相关代码,也就有了整理写出 ...

  9. C++11新特性总结 (二)

    1. 范围for语句 C++11 引入了一种更为简单的for语句,这种for语句可以很方便的遍历容器或其他序列的所有元素 vector<int> vec = {1,2,3,4,5,6}; ...

随机推荐

  1. 【研究】Discuz<3.4任意文件删除漏洞

    这里以Discuz3.2为例 关键字:Powered by Discuz! X3.2 时间有限,就不一一截图了,Discuz所有页面全在Discuz_X3.2_SC_UTF8/upload/目录下 利 ...

  2. 多重if 与 switch case的区别

    多重if:可以做等值操作也可以做区间操作 switch case:只能做等值操作

  3. ZABBIX 监控基本报警故障

    CPU触发器: 1)Processor load is too high on {HOST.NAME} {HOST.NAME}上处理器负载太高 触发器表达式:{Zabbix server:system ...

  4. GitHub(hexo)博客页面访问量错误以及中文乱码解决

    如果访问量不显示(乱码形状),是因为不蒜子域名更新,所以你的域名也需要更新 <script async src="//busuanzi.ibruce.info/busuanzi/2.3 ...

  5. sql server 远程备份 bak 删除

    前言: 管理一个公司的一个服务器,最近有一些维护SQLserver数据库活弄,写下防止忘了. 因为公司采用SQL\Redis\MongoDB共用,SQL用来存储基础的结构\权限\等一些杂七杂八的东西. ...

  6. JS 用正则表达式,验证密码包含数字和字母的方法

    必须包含至少一位数字和一位字母,脚本方法如下: function CheckPassWord(password) {//密码必须包含数字和字母 var str = password; if (str ...

  7. Linux systemd资源控制初探

    Linux systemd资源控制初探 本文记录一次cgroup子目录丢失问题,并简单探索了Linux systemd的资源控制机制. 问题现象 我们希望通过systemd拉起服务并通过cgroup限 ...

  8. 常用shell脚本(持续更新中)

    1. 写一个shell脚本来得到当前的日期,时间,用户名和当前工作目录. 答案 : 输出用户名,当前日期和时间,以及当前工作目录的命令就是logname,date,who i am和pwd. #!/b ...

  9. [转]Oracle ROWNUM用法和分页查询总结

    本文转自:http://blog.csdn.net/fw0124/article/details/42737671 ****************************************** ...

  10. 弹性布局学习-详解 align-items(四)

    目录 弹性布局学习-介绍(一)  弹性布局学习-详解 flex-direction[决定主轴的方向](二) 弹性布局学习-详解 justify-content(三) 弹性布局学习-详解 align-i ...