C++11新特性之 std::forward(完美转发)
我们也要时刻清醒,有时候右值会转为左值,左值会转为右值。
(也许“转换”二字用的不是很准确)
如果我们要避免这种转换呢?
我们需要一种方法能按照参数原来的类型转发到另一个函数中,这才完美,我们称之为完美转发。
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(完美转发)的更多相关文章
- C++11新特性之 std::forward(完美转发)(转)
我们也要时刻清醒,有时候右值会转为左值,左值会转为右值. (也许“转换”二字用的不是很准确) 如果我们要避免这种转换呢? 我们需要一种方法能按照参数原来的类型转发到另一个函数中,这才完美,我们称之为完 ...
- C++11新特性 变参模板、完美转发(简述)
变参模板 (Variadic Template) - 使得 emplace 可以接受任意参数,这样就可以适用于任意对象的构建 完美转发 - 使得接收下来的参数 能够原样的传递给对象的构造函数,这带来另 ...
- C++ 11新特性:std::future & std::shared_future) (转载)
上一讲<C++11 并发指南四(<future> 详解二 std::packaged_task 介绍)>主要介绍了 <future> 头文件中的 std::pack ...
- C++ 11新特性:std bind 原理简单图解(转载)
本文解释了bind 是如何工作的.为了清晰,我对图中的语法作了一些简化(例如,省略函数调用操作符的参数类型),并且简化了 bind 的实现. bind 可以用来将用户提供的需要一个参数的函数转换成不需 ...
- C++11新特性应用--实现延时求值(std::function和std::bind)
说是延时求值,注意还是想搞一搞std::function和std::bind. 之前博客<C++11新特性之std::function>注意是std::function怎样实现回调函数. ...
- [转载] C++11新特性
C++11标准发布已有一段时间了, 维基百科上有对C++11新标准的变化和C++11新特性介绍的文章. 我是一名C++程序员,非常想了解一下C++11. 英文版的维基百科看起来非常费劲,而中文版维基百 ...
- c++ 11 线程池---完全使用c++ 11新特性
前言: 目前网上的c++线程池资源多是使用老版本或者使用系统接口实现,使用c++ 11新特性的不多,最近研究了一下,实现一个简单版本,可实现任意任意参数函数的调用以及获得返回值. 0 前置知识 首先介 ...
- C++ 11学习和掌握 ——《深入理解C++ 11:C++11新特性解析和应用》读书笔记(一)
因为偶然的机会,在图书馆看到<深入理解C++ 11:C++11新特性解析和应用>这本书,大致扫下,受益匪浅,就果断借出来,对于其中的部分内容进行详读并亲自编程测试相关代码,也就有了整理写出 ...
- C++11新特性总结 (二)
1. 范围for语句 C++11 引入了一种更为简单的for语句,这种for语句可以很方便的遍历容器或其他序列的所有元素 vector<int> vec = {1,2,3,4,5,6}; ...
随机推荐
- HDU4499
In Chinese Chess, there is one kind of powerful chessmen called Cannon. It can move horizontally or ...
- KM算法(运用篇)
传送门:KM算法---理解篇 最佳匹配 什么是完美匹配 如果一个二分图,X部和Y部的顶点数相等,若存在一个匹配包含X部与Y部的所有顶点,则称为完美匹配. 换句话说:若二分图X部的每一个顶点都与Y中的一 ...
- 论文阅读 | STDN: Scale-Transferrable Object Detection
论文地址:http://openaccess.thecvf.com/content_cvpr_2018/papers/Zhou_Scale-Transferrable_Object_Detection ...
- pl/sql过期问题解决
第一步: 输入cmd进入命令窗口 命令窗口中输入 regedit HKEY_CURRENT_USER\Software\Allround Automations 删除Allround Automati ...
- zabbix web url监控
一, web监控 这个监控为通过cookie的值来监控网站是否能正常使用 这里测试环境为bbs网站 二, 配置web监控 01, 创建web监控项 02,配置步骤1 查看数据是否成功 第一查看首页时候 ...
- 学习javscript对象笔记(一)
对象(Objects) 对象属性值可以是除undefined值之外的任意值 1.对象字面量 a.{} b.new object(); 注意事项: 在对象字面量中,如果属性名是一个合法的javasc ...
- PHP 设置 socket连接
摘要: 作者博文地址:https://www.cnblogs.com/liu-shuai/ nginx和fastcgi的通信方式有两种,一种是TCP的方式,一种是unix socket方式. sock ...
- Coursera 机器学习 第6章(上) Advice for Applying Machine Learning 学习笔记
这章的内容对于设计分析假设性能有很大的帮助,如果运用的好,将会节省实验者大量时间. Machine Learning System Design6.1 Evaluating a Learning Al ...
- HDU 1394——Minimum Inversion Number——————【线段树单点增减、区间求和】
Minimum Inversion Number Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & ...
- 吴恩达《Machine Learning Yearning》总结(31-40章)
31.解读学习曲线:其他情况 下图反映了高方差,通过增加数据集可以改善. 下图反映了高偏差和高方差,需要找到一种方法来同时减少方差和偏差. 32.绘制学习曲线 情况:当数据集非常小时,比如只有100个 ...