上篇博客对右值、右值引用都做了简要介绍。

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

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

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.
}

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>(2); // rvalueint i = 1;auto p2 = make_unique1<A>(i); // lvaluestd::cout << "B\n";auto t = make_unique<B>(2, i, 3);
}
//输出:
rvalue overload, n=2
lvalue overload, n=1
B
rvalue overload, n=2
lvalue overload, n=1
rvalue overload, n=3

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

C++11新特性之 std::forward(完美转发)相关推荐

  1. C++11 新特性之std::thread

    C++11 新特性之std::thread 原文:https://blog.csdn.net/oyoung_2012/article/details/78958274 从C++11开始,C++标准库已 ...

  2. C++11新特性以及std::thread多线程编程

    一 .C++11新特性 1. auto 类型推导 1.1 当=号右边的表达式是一个引用类型时,auto会把引用抛弃,直接推导出原始类型: 1.2 当=号右边的表达式带有const属性时,auto不会使 ...

  3. C++11新特性之std::function std::bind

    std::function 在c++98&03时我们通常使用回调函数来处理一些需要某种场景下才触发的回调操作,但回调函数有一个限制就是它只能赋值给同类型的全局或者静态函数,对于其他有相同的返回 ...

  4. C++11新特性之std::bind()

    文章转载自:http://www.jellythink.com/archives/773 Cocos2d-x中有如下代码: // new callbacks based on C++11#define ...

  5. std:forward 完美转发

    概述:     // TEMPLATE CLASS identity template<class _Ty>     struct identity     {    // map _Ty ...

  6. C++ std::move/std::forward/完美转发

    右值引用相关的几个函数:std::move, std::forward 和 成员的 emplace_back; 通过这些函数我们可以避免不必要的拷贝,提高程序性能. move 是将 对象的状态 或者 ...

  7. C++11新特性之std::function<>

    转自http://www.jellythink.com/archives/771 看下面这两行代码: std::function<void(EventKeyboard::KeyCode, Eve ...

  8. C++11新特性之std::advance函数

    将某个迭代器前进到指定的位置上,例如: // advance example #include <iostream> // std::cout #include <iterator& ...

  9. c++11新特性的使用---可变模板参数、type_traits、function综合使用

    DLL帮助类 c++中手动调用dll是比较繁琐的,调用过程是:加载dll后还要定义一个对应的函数指针类型,接着调用GetProAddress获取函数地址,在转成函数指针,最后调用该函数如下例子: #i ...

最新文章

  1. linux安装jdk教程
  2. RIP路由协议快速入门(二)
  3. 《Oracle SQL疑难解析》——1.6 批量地从一个表中复制数据到另一个表
  4. python提取个十百千位数字_实现人脸识别、人脸68个特征点提取,或许这个 Python 库能帮到你!...
  5. 请求对象Request
  6. realloc()函数
  7. iframe在ipad safari的显示
  8. 3D建模学什么?细分的话,有这么多方向
  9. Django - Xadmin (四) Filter
  10. WebStorm 2017 最新版激活方式
  11. 一周第四次课(3月22日)
  12. bzoj 1054: [HAOI2008]移动玩具.cpp
  13. linux批量配置文件,linux下根据用户输入批量修改配置文件ip
  14. 猫途鹰:中国游客旅行花费低于全球平均水平,购物消费则远超全球
  15. 手动解析App dSYM示例
  16. java 毕向东 内部类_毕向东视频学习笔记【Java_Day10 多态+内部类】
  17. ARM 发布新一代 CPU 和 GPU,实现 20% 性能提升!
  18. AUTOSAR Network Wakeup(Can) Configuration
  19. 2017年西南民族大学程序设计竞赛-网络同步赛
  20. CTA入网认证业务办理

热门文章

  1. 【Zookeeper】JAVA通过ZK实现服务注册和服务发现
  2. 【ICPC-47】HDOJ 1865
  3. SketchUp教程 | 如何通过Skimp插件导入高质量模型、优化模型、匹配贴图?
  4. 杂散光分析与控制设计 课程大纲
  5. 【TES710D】基于复旦微的FMQL10S400全国产化ARM核心模块
  6. python 快速排序算法
  7. 抽象类,接口, final,选择排序
  8. @GetMapping
  9. pageInfo分页无效问题
  10. Q实验室与实验室之间对比对及有效性评价